pandas Plot出图
·
pandas绘图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.randn(10)
array([-0.55641654, 0.61721538, -0.44471859, -0.70549804, -0.40572372,
0.94200087, 0.57669463, 0.43246329, 1.10927002, -1.74735746])
Series
data=pd.Series(np.random.randn(300), index=np.arange(300))
data.plot()
plt.show()

#想看值的分布
#直方图/柱状图:看各值出现的频数
data.plot(kind='hist') #直方图 看出来是标准正态分布吧,均值为0
<matplotlib.axes._subplots.AxesSubplot at 0x11d7ea20>

#kde;Kernel Density Estimation plot #Kernel的密度估计图,主要对柱状图hist添加概率密度曲线
#概率密度曲线
data.plot(kind='kde')
<matplotlib.axes._subplots.AxesSubplot at 0x155dcd30>

#pie:饼图 pie doesn't allow negative values
data2=pd.Series([200,90,20,22,100],index="xi'an,chengdu,hangzhou,shenzhen,dali".split(','))
data2.plot(kind='pie',title='satisfied number')
plt.show()

help(data2.plot)
DataFrame
df= pd.DataFrame(np.random.randn(1000,4), columns=list('ABCD'))
df.head()
| A | B | C | D | |
|---|---|---|---|---|
| 0 | 0.100093 | 0.381531 | 0.072958 | -0.170367 |
| 1 | 0.745670 | -0.245102 | -1.570983 | -0.205432 |
| 2 | 1.130629 | 0.938289 | -0.088522 | 1.480945 |
| 3 | -1.221756 | -0.457804 | -0.956593 | -1.079864 |
| 4 | -1.009221 | 0.543376 | -0.578543 | 0.697118 |
df=df.cumsum()#累加在一起
df.head()
| A | B | C | D | |
|---|---|---|---|---|
| 0 | 0.100093 | 0.381531 | 0.072958 | -0.170367 |
| 1 | 0.845763 | 0.136429 | -1.498026 | -0.375799 |
| 2 | 1.976392 | 1.074718 | -1.586548 | 1.105147 |
| 3 | 0.754636 | 0.616914 | -2.543140 | 0.025283 |
| 4 | -0.254586 | 1.160290 | -3.121684 | 0.722401 |
df.plot()#绘制出A\B\C\D四条的变化曲线 横轴为0-1000条记录
plt.show()

df.plot(y=['A','B'])#绘制两条的
plt.show()
<matplotlib.axes._subplots.AxesSubplot at 0xeedc5f8>

绘制散点图
散点图的一个plot里的x,y size必须相同
df.plot(kind='scatter',x='A',y='B',label='Class1')
<matplotlib.axes._subplots.AxesSubplot at 0x295e4240>

df.plot(kind='scatter',x='A', y='C',s=df['D']*2,color='red',label='Class2')
<matplotlib.axes._subplots.AxesSubplot at 0x28796a20>

想把它们两个Class1 Class2放在同一个图里,怎么办呢
#错误error!
df.plot(kind='scatter', x='A', y=['B','C'])#ValueError: x and y must be the same size
#共同的x,不同的y
#先将Class1画一个在ax上,再画Class2在同一个ax上
ax=df.plot(kind='scatter',x='A',y='B',label='Class1')
ax1=df.plot(kind='scatter',x='A', y='C',color='darkblue',label='Class2',ax=ax)

#我可不可以在图上再来一个Class3,试一试
df.plot(kind='scatter', x='A', y='D', color='green',label='Class3',ax=ax1) #是label not lable
plt.show()
help(df.plot)
df.plot(kind=“scatter”) 等价于 df.plot.scatter()
xticks : sequence 可设置x轴刻度 如,x轴:xsticks=[0,10,20,30,40,50]
| Values to use for the xticks
| yticks : sequence
| Values to use for the yticks
| xlim : 2-tuple/list
| ylim : 2-tuple/list 可设置y轴坐标范围 如,y轴 ylim=(-1,1)
scatter(self, x, y, s=None, c=None, **kwds)
s:size嘛 scalar or array_like
每一个点的大小。值可以是:
一个标量,所有点大小相同;
标量的序列,每个点可以获取不同的值。
c:color嘛 str, int or array_like
每个点的颜色。值可以是:
一个单一的颜色。如"red"或者"#a98d19"
颜色的序列。如[“green”,“darkblue”]
更多推荐
所有评论(0)