一、绘图步骤 以散点图为例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['axes.facecolor']='#0D404D'
plt.rcParams['axes.edgecolor']='white'
plt.rcParams['xtick.color']='white'
plt.rcParams['ytick.color']='white'
font={'family':'SimHei',
'weight':'normal',
'size':12,
'color': 'white'
}
plt.figure(facecolor='#0D404D',figsize=(8,8))
x=np.arange(0,2*np.pi,0.01)
y=np.sin(x)
plt.scatter(x,y,s=100,c='c',alpha=0.3,edgecolors='hotpink',linewidths=1.2)
plt.xlabel('x轴',fontdict=font)
plt.ylabel('y轴',rotation=90,fontdict=font)
plt.yticks([-1,0,1])
plt.title('scatter',fontdict=font)
plt.savefig('scatter.png')
plt.show()
二、多个图形绘制
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['axes.facecolor']='#0D404D'
plt.rcParams['axes.edgecolor']='white'
plt.rcParams['xtick.color']='white'
plt.rcParams['ytick.color']='white'
font={'family':'SimHei',
'weight':'normal',
'size':12,
'color': 'white'
}
plt.suptitle('我的画板标题',fontdict=font)
plt.subplots_adjust(left=0.1,hspace=0.8,top=0.9)
'''
hspace:上下子图之间的间距
wspace:左右子图之间的间距
'''
p1=plt.figure(figsize=(6,18),facecolor='#0D404D')
x=np.arange(0,2*np.pi,0.1)
p1.add_subplot(3,2,1)
plt.scatter(np.sin(x),np.cos(x))
p1.add_subplot(6,2,2)
plt.scatter(np.sin(x),np.cos(x))
p1.add_subplot(6,2,4)
plt.scatter(np.sin(x),np.cos(x))
p1.add_subplot(3,1,2)
plt.scatter(np.sin(x),np.cos(x))
p1.add_subplot(3,2,5)
plt.scatter(np.sin(x),np.cos(x))
p1.add_subplot(3,2,6)
plt.scatter(np.sin(x),np.cos(x))
plt.savefig('hhhhh.png')
plt.show()
三、不规则子图绘制
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.gridspec import GridSpec
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['axes.facecolor']='#0D404D'
plt.rcParams['axes.edgecolor']='white'
plt.rcParams['xtick.color']='white'
plt.rcParams['ytick.color']='white'
font={'family':'SimHei',
'weight':'normal',
'size':12,
'color': 'white'
}
plt.subplots_adjust(hspace=0.4,wspace=0.6)
plt.figure(figsize=(8,8),facecolor='#0D404D')
gs=GridSpec(8,8)
plt.subplot(gs[:7,:1])
x=np.arange(0,2*np.pi,0.1)
y=np.sin(x)
plt.scatter(x,y)
plt.subplot(gs[:3,1:])
y1=np.cos(x)
plt.scatter(x,y1)
plt.subplot(gs[3:7,1:5])
plt.scatter(x,y)
plt.subplot(gs[3:6,5:])
plt.scatter(x,y)
plt.subplot(gs[6:7,5:])
plt.scatter(x,y)
plt.subplot(gs[-1,:])
plt.scatter(x,y)
plt.savefig('lll.png')
plt.show()
四、其他图形绘制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['axes.edgecolor']='blue'
plt.rcParams['xtick.color']='blue'
plt.rcParams['ytick.color']='blue'
font={'family':'SimHei',
'weight':'normal',
'size':12,
'color': 'white'
}
(一)折线图
plt.figure(facecolor='#0D404D',figsize=(8,8))
x=np.arange(1,11,1)
y=2*x+1
plt.plot(x,y,
color='y',
marker='D',
linestyle=':',
markerfacecolor='m',
markersize=10,
markeredgecolor='hotpink',
alpha=0.4 )
(二)柱状图
plt.figure(facecolor='#0D404D')
x=[1,2,3]
y=[90,88,60]
plt.bar(x,y)
plt.xticks(x,['python0831','python0903','python0330'])
for x,y in zip(x,y):
plt.text(x,y,y,fontdict=font,ha='center',va='center',bbox={'facecolor':'red','alpha':0.5,'edgecolor':'yellow'})
柱状图多图对比
plt.figure()
x1=[1,3]
y1=[1000,800]
plt.bar(x1,y1,color='green',edgecolor='black',linewidth=2)
x2=[2,4]
y2=[300,100]
plt.bar(x2,y2,color='red')
plt.xticks([1.5,3.5],[2019,2020])
for x1,y1 in zip(x1,y1):
plt.text(x1,y1,y1,ha='center')
for x2,y2 in zip(x2,y2):
plt.text(x2,y2,y2,ha='center')
plt.legend(['男','女'],facecolor='gray')
plt.savefig('1.png')
plt.show()
(三)直方图
plt.figure()
x=np.random.randn(10000)
plt.hist(x,bins=9)
plt.show()
(四)饼图
plt.figure(figsize=(6,6))
x=[1000,2000,3000]
labels=['A','B','C']
plt.pie(x,
labels=labels,
autopct='%1.1f%%',
explode=[0.1,0.02,0],
shadow=True,
center=(1,1),
)
plt.show()
(五)雷达图
x=np.linspace(0,2*np.pi,3,endpoint=False)
x_concat=np.concatenate((x,[x[0]]))
y=[2,1,3,2]
plt.figure(figsize=(6,6))
plt.polar(x_concat,y,marker='s')
plt.xticks(x)
plt.fill(x_concat,y,'pink')
plt.show()
x=np.linspace(2*np.pi/20,2*np.pi*(21/20),10,endpoint=False)
x_concat=np.concatenate((x,[x[0]]))
y=[3,1,3,1,3,1,3,1,3,1,3]
plt.figure(figsize=(6,6))
plt.polar(x_concat,y,marker='s')
plt.xticks(x)
plt.fill(x_concat,y,'pink')
plt.show()