时间:2022-09-02 08:28:45 | 栏目:Python代码 | 点击:次
Pandas的DataFrame和Series在Matplotlib基础上封装了一个简易的绘图函数,使得数据处理过程中方便可视化查看结果。
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot() plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='bar') plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='barh') plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='bar',stacked=True) plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='barh',stacked=True) plt.show()
数据通常是一些点的集合
常用来绘制各种相关性,适合研究不同变量间的关系
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=['A','B']) df.plot(kind='scatter',x='A',y='B',s=df.A*100,c='red') plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.Series(3*np.random.rand(4),index=['a','b','c','d']) df.plot.pie(figsize=(6,6)) plt.show()
体现数据出现的次数
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(1000,2),columns=['a','b']) df.plot.hexbin(x='a',y='b',sharex=False,gridsize=30) plt.show()
基于最小值、上四分位、中位数、下四分位和最大值5个数值特征展示数据分布的标准方式,可以看出数据是否具有对称性,适用于展示一组数据的分布情况
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(1000,2),columns=['a','b']) df.plot(y=df.columns,kind='box',vert=False) plt.show()
subplots:默认False 若每列绘制子图就为True
layout:子图布局
figsize:画布大小
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(5,2),columns=['a','b']) df.plot(subplots=True,layout=(2,3),figsize=(10,10),kind='bar') plt.show()