利用matplotlib制作折线图

利用matplotlib制作折线图

导入库/包:
import numpy as np
from matplotlib import pyplot as plt
代码块:
x = range(1,8) #x轴的位置
y = [17,17,18,15,11,11,13]
#传入x和y,通过plot画折线图
# plt.plot(x,y)
#折线图的颜色和形状设置
plt.plot(x,y,color='red',alpha=0.5,linestyle='-.',linewidth=3)
#折点样式
plt.plot(x,y,marker='o',color='red',markersize='20',markeredgecolor='g',markeredgewidth = 5)
plt.show()

基础属性设置

  • alpha=0.5 : 折线的透明度(0-1)
  • linestyle=’–’ : 折线的样式
  • linewidth=3 : 折线的宽度

线的样式
实线(solid)( - )
短线(dashed)( – )
短点相间线(dashdot)( -.)
虚点线(dotted)( :)

折点样式
折点样式:marker=‘o’
折点大小:markersize=‘20’
折点颜色:markeredgecolor=‘g’
折点外围大小:markeredgewidth = 5
折点形状选择:
-’ ‘’ ‘-.’ ‘:’ ‘.’ ‘,’ ‘o’ ‘v’ ‘^’ ‘<’ ‘>’ ‘1’ ‘2’ ‘3’ ‘4’ ‘s’ ‘p’ ’ * ’ ‘h’ ‘H
+’ ‘x’ ‘D’ ‘d’ ‘|’ ‘_
在这里插入图片描述
设置图片的大小和保存
设置图片的大小:
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是21*30cm的纸张

from matplotlib import pyplot as plt
import random
x = range(2,26,2) #x轴的位置
y = [random.randint(15,30) for i in x]
#设置图片大小
#设置画布对象
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
plt.show()
  • 保存 plt.show() (注意: 要放在绘制的下面,并且plt.show()会释放figure资源,如果在显示图像之后保存图片将只能保存空图片。)
  • 图片的格式也可以保存为svg这种矢量图格式,这种矢量图放在网页中放大后不会有锯齿。

绘制x轴和y轴的刻度

from matplotlib import pyplot as plt
import random
x = range(2,26,2) #x轴的位置
y = [random.randint(15,30) for i in x]
plt.figure(figsize=(20,8),dpi=80)
#设置x轴的刻度
plt.xticks(x)
plt.xticks(range(1,25))
#设置y轴刻度
plt.yticks(y)
plt.yticks(range(min(y),max(y)+1))
#构造x轴刻度标签
x_ticks_label=["{}:00".format(i) for i in x]
#rotation=45 让字旋转45度
plt.xticks(x,x_ticks_label,rotation=45)
#设置x轴的刻度标签
y_ticks_label=["{}:℃".format(i) for i in range(min(y),max(y)+1)]
plt.yticks(range(min(y),max(y)+1),y_ticks_label)
#绘图
plt.plot(x,y)
plt.show()

在这里插入图片描述
设置显示中文
matplotlib只显示英文,无法显示中文,需要修改matplotlib的默认字体
通过matplotlib下的font_manager可以解决

from matplotlib import pyplot as plt
from matplotlib import font_manager
import random
x = range(0,120)
y = [random.randint(10,30) for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
my_font=font_manager.FontProperties(fname='F:/Python/maozedong.ttf',size=18)
#rotation=45 让字旋转45度
plt.xlabel('时间',rotation=45,fontproperties=my_font)
plt.ylabel('次数',fontproperties=my_font)
#设置标题
plt.title('每分钟跳动次数',fontproperties=my_font,color='red')
plt.show()

在这里插入图片描述
可以自己下载字体文件(xxx.ttf),然后双击安装即可
rotation将字体旋转多少度
xlabel 设置x轴的中文
ylabel 设置y轴的中文
title 设置图表的标题
font_manager.FontProperties 设置字体的路径,大小

一线多图

import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
'''
统计你和同事从11岁到30岁每年交的男/女朋友的数量
列表y1和y2,请在一个图中绘制该数据的折线图
分析每年交朋友的数量走势
'''
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,3,4,3,2,1,2,1,1,1,1,1,1,1,1]
x=range(11,31)
#设置图形
plt.figure(figsize=(20,8),dpi=80)
#设置图例
plt.plot(x,y1,color='red',label='自己')
plt.plot(x,y2,color='green',label='同事')
#设置x轴刻度
xtick_labels=['{}岁'.format(i) for i in x]
#字体
my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/simkai.ttf', size=18)
#rotation倾斜45度
plt.xticks(x,xtick_labels,fontproperties=my_font,rotation=45)
'''
图例,注意:只有在这里需要添加prop参数是显示中文,其他的都用fontproperties
设置位置:upper left、lower left、center left、upper center
图例用legend
'''
plt.legend(prop=my_font,loc='upper right')
# x标题
plt.xlabel('时间',fontproperties=my_font)
# y标题
plt.ylabel('次数',fontproperties=my_font)
#标题
plt.title('时间/次数',fontproperties=my_font,color='red')
#绘制网格
#alpha 设置透明度
plt.grid(alpha=0.4)
plt.show()

在这里插入图片描述一图多个坐标系子图

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1, 100)
#新建figure对象
fig=plt.figure(figsize=(20,10),dpi=80)
#新建子图1
#add_subplot方法----给figure新增子图
ax1=fig.add_subplot(2,2,1)
ax1.plot(x, x)
#新建子图2
ax2=fig.add_subplot(2,2,2)
ax2.plot(x, x ** 2)
ax2.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#新建子图3
ax3=fig.add_subplot(2,2,3)
ax3.plot(x, np.log(x))
plt.show()

add_subplot方法:给figure新增子图
在这里插入图片描述

发布了49 篇原创文章 · 获赞 76 · 访问量 2711

猜你喜欢

转载自blog.csdn.net/qq_39783601/article/details/104554197
今日推荐