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()
ABCD
00.1000930.3815310.072958-0.170367
10.745670-0.245102-1.570983-0.205432
21.1306290.938289-0.0885221.480945
3-1.221756-0.457804-0.956593-1.079864
4-1.0092210.543376-0.5785430.697118
df=df.cumsum()#累加在一起
df.head()
ABCD
00.1000930.3815310.072958-0.170367
10.8457630.136429-1.498026-0.375799
21.9763921.074718-1.5865481.105147
30.7546360.616914-2.5431400.025283
4-0.2545861.160290-3.1216840.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”]

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐