绘图与可视化

一.matplotlib

import numpy as np
import matplotlib.pyplot as plt
data = np.arange(10)
plt.plot(data)
plt.show()	#一定要写,不然显出不出来图片

2.子图:figure+add_subplot , subplots

1.figure, add_subplot
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
data = np.arange(10)
plt.plot(data) #绘制命令会在最近的子图上呈现

ax2 = fig.add_subplot(2,2,2)
plt.show()

2.subplots
fig, axes = plt.subplots(2,2) #会生成关于子图的数组将其存放在axes中
data = np.arange(10)
axes[0,0].plot(data) #通过axes[0,0]来把图像放在第一个子图中
plt.show()

3.颜色(color),标记(marker),线类型(linestyle),图例(label):在plot中设置,和MatLab差不多

4.标题,标签

fig, axes = plt.subplots(2,2)
data = np.random.randn(10)
data2 = np.random.randn(10)
axes[0,0].plot(np.arange(0,100,10),data.cumsum(),'go--',label='one')
axes[0,0].plot(np.arange(0,100,10),data2.cumsum(),'ro--',label='two')
axes[0,0].legend(loc='best')

#利用字典形式设置标题和行列标签
pr={
    
    
    'title':'zxw',
    'xlabel':'x',
    'ylabel':'y'
}
#set接受字典,批量的设置绘图属性
axes[0,0].set(**pr)
plt.show()

二.pandas+seaborn

1.折线图

Series和DataFrame都有plot属性,默认情况下,绘制的就是折线图

a = Series(np.random.randn(5))
print(a.plot())
plt.show()

b = DataFrame(np.random.randn(10,4),
              columns=['A','B','C','D'],
              index=np.arange(0,100,10))
b.plot(title='zxw')
plt.show()

2.柱状图:plot.bar(垂直柱状图),plot.barh(水平柱状图)

3.直方图:plot.hist

4.密度图:plot.density

猜你喜欢

转载自blog.csdn.net/qq_41458842/article/details/102757550