From the previous section, matplotlib is actually a relatively low-level tool. To assemble a chart, it took its various basic components of the job.
There are many pandas can use DataFrame organizational characteristics of the object data to create a standard chart of advanced drawing methods.
1. FIG linear
Series and DataFrame have a method for generating a plot of various types of charts. By default, they generate a line chart.
fig4 = plt.figure()
s = Series(np.random.randn(10).cumsum(), index = np.arange(0, 100, 10))
s.plot()
Figure:
Series.plot method parameters:
The plot DataFrame method draws a line for each column in a subplot and automatically creates a legend.
df = DataFrame(np.random.randn(10, 4).cumsum(0),
columns=['A', 'B', 'C', 'D'],
index = np.arange(0, 100, 10))
df.plot()
如图:
专用于DataFrame 的plot 的参数:
2. 柱状图
1)在生成线型图的代码中加上 kind=‘bar’(垂直柱状图)或kind='barh'(水平柱状图)即可生成柱状图。
fig5,axes = plt.subplots(2, 1)
data = Series(np.random.rand(16), index =list('abcdefghijklmnop'))
data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
data.plot(kind='barh', ax=axes[0], color='k', alpha=0.7)
如图:
2)对于DataFrame ,柱状图会将每一行的值分为一组:
df = DataFrame(np.random.rand(6, 4),
index = ['one','two','three','four','five','six'],
columns = pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
结果:
注:DataFrame 各列的名称“Genus” 被用作了图例的标题。
3)生成堆积柱状图
设置stacked = True 即可。
df.plot(kind = 'barh', stacked = True, alpha = 0.5)
如图:
3. 直方图和密度图
直方图(histogram)是一种可以对值频率进行离散化显示的直方图。数据点被拆分到离散的、间隔均匀的面元中,绘制的是各面元中数据点的数量。
4. 散布图
散布图(scatter plot)是观察两个一维数据序列之间的关系的有效手段。