36.Pandas绘图操作

%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#绘制折线图
s = pd.Series(np.random.randn(10),index=np.arange(0,100,10))
s.plot()
<matplotlib.axes._subplots.AxesSubplot at 0xae43e10>

#cumsum(axis),前面元素的累加和
df = pd.DataFrame(np.random.randn(10,4).cumsum(0),index = np.arange(0,100,10),columns=['A',"B","C","D"])
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x8c85518>

#绘制柱状图
data = pd.Series(np.random.randn(16),index=list('abcdefghijklmnop'))
#两行一列的子图
fig,axes = plt.subplots(2,1)
#ax为图表显示的位置,kind为bar,柱状图
data.plot(ax=axes[0],kind='bar')
#kind为barh,横向柱状图
data.plot(ax=axes[1],kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0xa34b518>

df = pd.DataFrame(np.random.rand(6,4),index=['one','two','three','four','five','six'],columns=pd.Index(['A','B','C','D'],name="Genus"))
#绘制柱状图
df.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0xa45e5c0>

#绘制直方图
df.A.plot(kind='hist',bins=50)

#散点图
df.plot.scatter('A','B')
#混合散点图,数据维度越大,越复杂
pd.plotting.scatter_matrix(df,color='g',alpha=0.3)
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000AA338D0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AA685C0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AA89B00>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AABB1D0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000AAE2860>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AAE2898>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AB3D5C0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AB63C50>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000AB96320>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000ABBD9B0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000ABF1080>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AC17710>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000AC41DA0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AC72438>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000AC99AC8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000000ACCC198>]],
      dtype=object)

猜你喜欢

转载自blog.csdn.net/xzy53719/article/details/82788340