
1.Matplotlib画图简单实现
Matplotlib在一个绘制2D图片的库
import matplotlib.pyplot as plt
plt.plot([1,0,9],[4,5,6])
plt.show()
 
2.折线图
2.1绘制折线图
from matplotlib import pyplot as plt
x=range(0,8)
y=[14,17,19,11,14,13,15,16]
plt.plot(x,y)
plt.show()

接下来逐步对折线图进行修改
2.2设置线的格式
plt.plot(x,y,color='red',alpha=0.5,linestyle='--',linewidth=1)
plt.show()
'''线的样式
- 实线(solid)
-- 短线(dashed)
-. 短点相间图
: 虚电线(dotted)
'''

2.3设置折点
plt.plot(x,y,marker='o',markersize=10,markerfacecolor='red',markeredgecolor='red',markedgewidth=5)

2.4.图片的保存和导出
import random
x=range(2,26,2)
y=[random.randint(10, 16) for i in x]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y,color='yellow',alpha=0.8,marker='o',linestyle='--',linewidth=1)
plt.savefig('python初阶\案例图片.png')

2.5设置刻度
import random
from matplotlib import font_manager
x=range(2,26,2)
y=[random.randint(15,30) for i in x]
plt.figure(figsize=(20,8),dpi=80)
x_ticks_label=["{}:00".format(i) for i in x]
plt.xticks(x,x_ticks_label,rotation=45)
y_ticks_label=['{}摄氏度'.format(i) for i in range(min(y),max(y)+1)]
plt.xlabel("时间")
plt.ylabel("温度")
plt.yticks(range(min(y),max(y)+1),y_ticks_label)
plt.plot(x,y,color='red',alpha=0.8,marker='o',linestyle='--',linewidth=1)
plt.show()

2.6显示中文
matplotlib只显示应为,无法显示中文,需要修改matplotlib的默认字体
通过matplotlib下的font_manger可以解决
import random
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF",size=20)
x=range(2,26,2)
y=[random.randint(15,30) for i in x]
plt.figure(figsize=(20,8),dpi=80)
x_ticks_label=["{}:00".format(i) for i in x]
plt.xticks(x,x_ticks_label,rotation=45)
y_ticks_label=['{}摄氏度'.format(i) for i in range(min(y),max(y)+1)]
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度",fontproperties=my_font)
plt.yticks(range(min(y),max(y)+1),y_ticks_label,fontproperties=my_font)
plt.plot(x,y,color='red',alpha=0.8,marker='o',linestyle='--',linewidth=1)
plt.title("温度表",fontproperties=my_font,color='red')
plt.show()

2.7实例:每分钟心脏跳动图
from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF",size=20)
x=range(0,121)
y=[random.randint(10,30) for i in x]
plt.xlabel("时间",fontproperties=my_font,rotation=45)
plt.ylabel("次数",fontproperties=my_font)
plt.plot(x,y,color='blue',linewidth=2,alpha=0.7)
plt.title("每分钟心脏跳动数",color='red',fontproperties=my_font)
plt.show()
 
3.拓展
3…1一图多线
from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
my_font=font_manager.FontProperties(fname='C:\Windows\Fonts\C:\Windows\Fonts\SIMLI.TTF',size=20)
x=range(11,31)
y1=[1,0,1,1,2,4,3,4,4,5,6,5,4,3,3,1,1,1,1,1]
y2=[1,0,3,1,2,2,2,4,6,4,8,4,3,5,6,4,1,4,2,5]
y3=[2,1,4,0,4,3,7,3,1,0,5,1,3,4,1,4,5,3,2,3]
x_ticks_label=['{}岁'.format(i) for i in x]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y1,color='red',label='自己',zorder=5)
plt.plot(x,y2,color='blue',label='同事李',zorder=10)
plt.plot(x,y3,color='green',label='同事张',zorder=15)
y_ticks_label=['{}个'.format(i) for i in range(0,9)]
plt.yticks(range(0,9),y_ticks_label,fontproperties=my_font)
plt.xticks(x,x_ticks_label,fontproperties=my_font,rotation=45)
plt.grid(alpha=0.5)
plt.legend(prop=my_font,loc='upper right')
plt.show()

3.2一图多个坐标子图
import numpy as np
x=np.arange(1,100)
fig=plt.figure(figsize=(20,8),dpi=80)
ax1=fig.add_subplot(2,2,1)
plt.plot(x,x)
ax2=fig.add_subplot(2,2,2)
plt.plot(x,x**2)
ax3=fig.add_subplot(2,2,3)
plt.plot(x,np.log(x))
plt.show()

对纵坐标进行改进
y=np.arange(-10,10)
z=y**2
plt.plot(y,z)
plt.xlim([-5,5])
plt.ylim([0,80])
plt.show()

y=np.arange(-10,10)
z=y**2
plt.plot(y,z)
plt.xlim(xmax=4)
plt.show()

3.3坐标轴设置
x = [-3,-2,-1,0,1,2,3]
y = range(0,14,2)
ax = plt.gca()
plt.plot(x,y)
plt.show()

x = [-3,-2,-1,0,1,2,3]
y = range(0,14,2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('red')
ax.spines['left'].set_color('green')
plt.plot(x,y)
plt.show()

设置为标准坐标轴
x = [-3,-2,-1,0,1,2,3]
y = range(0,14,2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('red')
ax.spines['left'].set_color('green')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.plot(x,y)
plt.show()
 
|