Pandas的另一个优点是与Matplotlib的集成,可以直接绘制DataFrame和Series。
注意:如没有安装Matplotlib,需首先安装:
pip install matplotlib。
接下来开始绘图。
首先画一下电影的评分和票房之间的关系,调用movies_df上的.plot()方法:
# 加载数据 movies_df = pd.read_csv("IMDB-Movie-Data.csv", index_col="Title") movies_df.columns = ['rank', 'genre', 'description', 'director', 'actors', 'year', 'runtime', 'rating', 'votes', 'revenue_millions', 'metascore'] # 绘图 movies_df.plot(kind='scatter', x='rating', y='revenue_millions', title='Revenue (millions) vs Rating') plt.show()
输出

如果想要绘制单个列的简单直方图,可以调用列上的plot:
movies_df['rating'].plot(kind='hist', title='Rating')
输出

回忆一下describe()在rating列中给出的统计信息:
movies_df['rating'].describe()
输出
count 1000.000000 mean 6.723200 std 0.945429 min 1.900000 25% 6.200000 50% 6.800000 75% 7.400000 max 9.000000 Name: rating, dtype: float64
使用箱线图,我们可以可视化这些数据:
movies_df['rating'].plot(kind="box")
输出


箱线图说明
通过组合rating_category和revenue_millions,可以创建一个票房箱线图:
movies_df.boxplot(column='revenue_millions', by='rating_category')
输出

本文来自博客园,作者:大码王,转载请注明原文链接:https://www.cnblogs.com/huanghanyu/
posted on
浙公网安备 33010602011771号