条形图与散点图

  1. 取出一行数据

    import pandas as pd
    reviews = pd.read_csv('fandango_scores.csv')
    cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
    norm_reviews = reviews[cols]
    print(norm_reviews[:1])
    

    运行结果:
    在这里插入图片描述

  2. 显示柱形图

    import matplotlib.pyplot as plt
    from numpy import arange
    #The Axes.bar() method has 2 required parameters, left and height. 
    #We use the left parameter to specify the x coordinates of the left sides of the bar. 
    #We use the height parameter to specify the height of each bar
    num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
    
    bar_heights = norm_reviews.loc[0, num_cols].values
    #print bar_heights
    bar_positions = arange(5) + 0.75
    #print bar_positions
    fig, ax = plt.subplots()
    ax.bar(bar_positions, bar_heights, 0.5)
    plt.show()
    

    运行结果:
    在这里插入图片描述

  3. 设置坐标名称

    #By default, matplotlib sets the x-axis tick labels to the integer values the bars 
    #spanned on the x-axis (from 0 to 6). We only need tick labels on the x-axis where the bars are positioned. 
    #We can use Axes.set_xticks() to change the positions of the ticks to [1, 2, 3, 4, 5]:
    
    num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
    bar_heights = norm_reviews.loc[0, num_cols].values
    bar_positions = arange(5) + 0.75
    tick_positions = range(1,6)
    fig, ax = plt.subplots()
    
    ax.bar(bar_positions, bar_heights, 0.5)
    ax.set_xticks(tick_positions)
    ax.set_xticklabels(num_cols, rotation=45)
    
    ax.set_xlabel('Rating Source')
    ax.set_ylabel('Average Rating')
    ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
    plt.show()
    

    运行结果:
    在这里插入图片描述

  4. 改变图的方向

    import matplotlib.pyplot as plt
    from numpy import arange
    num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
    
    bar_widths = norm_reviews.loc[0, num_cols].values
    bar_positions = arange(5) + 0.75
    tick_positions = range(1,6)
    fig, ax = plt.subplots()
    ax.barh(bar_positions, bar_widths, 0.5)
    
    ax.set_yticks(tick_positions)
    ax.set_yticklabels(num_cols)
    ax.set_ylabel('Rating Source')
    ax.set_xlabel('Average Rating')
    ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')
    plt.show()
    

    运行结果:
    在这里插入图片描述

  5. 散点图

    #Let's look at a plot that can help us visualize many points.
    fig, ax = plt.subplots()
    ax.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
    ax.set_xlabel('Fandango')
    ax.set_ylabel('Rotten Tomatoes')
    plt.show()
    

    运行结果:
    在这里插入图片描述

  6. 添加子图

    #Switching Axes
    fig = plt.figure(figsize=(5,10))
    ax1 = fig.add_subplot(2,1,1)
    ax2 = fig.add_subplot(2,1,2)
    ax1.scatter(norm_reviews['Fandango_Ratingvalue'], norm_reviews['RT_user_norm'])
    ax1.set_xlabel('Fandango')
    ax1.set_ylabel('Rotten Tomatoes')
    ax2.scatter(norm_reviews['RT_user_norm'], norm_reviews['Fandango_Ratingvalue'])
    ax2.set_xlabel('Rotten Tomatoes')
    ax2.set_ylabel('Fandango')
    plt.show()
    

    运行结果:
    在这里插入图片描述

发布了301 篇原创文章 · 获赞 30 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42260102/article/details/103439433