300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > plot(matplotlib.pyplot)绘图(条状图)

plot(matplotlib.pyplot)绘图(条状图)

时间:2020-09-22 18:03:52

相关推荐

plot(matplotlib.pyplot)绘图(条状图)

使用数据地址:链接:/s/1wXtZRDcM-JKk_dIDRyd_dg?pwd=pyth

提取码:pyth

提取一些数据,用来绘图用

import pandas as pdreviews = pd.read_csv('fandango_scores.csv')cols = ['FILM', 'RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']#取一些列做新的数据,file列是电影名,后面的都是媒体名,媒体给电影评分。norm_reviews = reviews[cols]#取一些列做新的数据norm_reviewsprint(norm_reviews[0:2])'''FILM RT_user_norm Metacritic_user_nom \0 Avengers: Age of Ultron () 4.3 3.55 1Cinderella () 4.0 3.75 IMDB_norm Fandango_Ratingvalue Fandango_Stars 0 3.90 4.5 5.0 1 3.55 4.5 5.0 '''#每个媒体的评分都绘制成一个柱子import matplotlib.pyplot as pltfrom 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 barnum_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']print(norm_reviews.loc[0, num_cols])#取第0行的数据来绘图'''RT_user_norm 4.3Metacritic_user_nom3.55IMDB_norm3.9Fandango_Ratingvalue4.5Fandango_Stars 5.0Name: 0, dtype: object'''bar_heights = norm_reviews.loc[0, num_cols].values#取第0行,num_cols中列的数值,此数据用来作为条状图的条高print (bar_heights)#[4.3 3.55 3.9 4.5 5.0]bar_positions = arange(5) + 0.75#表示的是每个柱子的中间位置距离左边原点的距离bar_positions = arange(5) + 0.75#表示的是每个柱子的中间位置距离左边原点的距离print(bar_positions)#[0.75 1.75 2.75 3.75 4.75]fig, ax = plt.subplots()#subplot函数返回一个figure,一个axax.bar(bar_positions, bar_heights, 0.5)#0.5表示的是柱子的粗细plt.show()

这种图像过于简洁,增加x、y轴描述,图像标题等,需对ax对象进行操作,每个柱子(条)均可定义名称,需对ax对象使用set_xticklabels函数。

#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#取得第0行,num_cols中的数据用来绘图tick_positions = range(1,6)#1, 2, 3, 4, 5fig, ax = plt.subplots()ax.bar(bar_positions, bar_heights, 0.5)#传入条状图参数,条位置、条高、条的粗细ax.set_xticks(tick_positions)#传入参数tick_positions作为每个条的位置1, 2, 3, 4, 5ax.set_xticklabels(num_cols, rotation=45)#使用set_xticklabels函数对每个条定义名称,num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars'],rotation为旋转角度。ax.set_xlabel('Rating Source')ax.set_ylabel('Average Rating')ax.set_title('Average User Rating For Avengers: Age of Ultron ()')plt.show()

以上柱形图均为竖立型柱形图,想要画出横着的柱形图,需使用函数barh

import matplotlib.pyplot as pltfrom numpy import arangenum_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']bar_widths = norm_reviews.loc[0, num_cols].valuesbar_positions = arange(5) + 0.75tick_positions = range(1,6)fig, ax = plt.subplots()ax.barh(bar_positions, bar_widths, 0.5)#横着画柱形图,需使用barh函数,而非bar函数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 ()')plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。