折线图

标记格式大小设置:https://blog.csdn.net/qq_40260867/article/details/95310956

1 plt.plot( )

与画柱状图类似,只不过将bar( )函数改成了plot( )

#!/usr/bin/env.python
#*._ * _.coding: utf - 8.
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
zhfont= mpl.font_manager.FontProperties(fname='/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc')
a = [2,43,56,78,54,67,23]
b = [66,51,13,24,31,13,42]
c = ['山西','河北','北京','河南','江苏','海南','四川']
plt.figure(figsize=(10, 5.5))
# 背景是网格图
plt.grid(alpha=0.4,linestyle='--')
# 设置绘图风格
plt.style.use('ggplot')
# 参数
# marker: 设置节点出的标记样式  https://blog.csdn.net/weisiyu_liang/article/details/99746723
# markeredgecolor 或 mec 标记边缘颜色
# markeredgewidth 或 mew 标记边缘宽度
# markerfacecolor 或 mfc 标记面颜色
plt.plot( c, a, color="red", marker = 'v',  linewidth=1,label = 'a')
plt.plot( c, b, color="blue", marker = 'o', linewidth=1,label = 'b')
# annotate()给折线点设置坐标值
# s:  要注释的文本内容
# xy: 被注释的坐标点
# xytext: 要注释文本的坐标位置
# xycoords:被注释点的坐标系属性
# textcoords: 注释文本的坐标系属性,默认与xycoords属性值相同,也可设为不同的值。
# 除了允许输入xycoords的属性值,还允许输入以下两种:'offset pixels'和'offset points'
# 参考:https://blog.csdn.net/leaf_zizi/article/details/82886755

# 下面是两种注释方法
for p,q in zip(c,a):
    plt.annotate( s = '(%s,%s)'%(p,q),xy=(p,q),xytext=(-20,10),
                 textcoords='offset points')
    # linux中无法显示汉字时,可以用text
    plt.text(p,q+2, '({},{})'.format(p,q), fontproperties=zhfont)
# xlabel、ylabel:分别设置X、Y轴的标题文字。
plt.xlabel("训练次数",fontsize=15, fontproperties=zhfont)
plt.ylabel("准确率",fontsize=15, fontproperties=zhfont)
# title:设置子图的标题。
plt.title("折线图", fontproperties=zhfont, fontsize=25)
# xlim、ylim:分别设置X、Y轴的显示范围。
plt.ylim(0, 80)
# 保存图片
# plt.savefig('quxiantu.png', dpi=120, bbox_inches='tight')
plt.legend()
plt.show()
View Code

2 plt.stem( )

#!/usr/bin/env.python
#*._ * _.coding: utf - 8.
from pylab import *
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
zhfont= mpl.font_manager.FontProperties(fname='/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc')
a = [2,43,56,78,54,67,23]
b = [66,51,13,24,31,13,42]
c = [1,2,3,4,5,6,7]
plt.figure(figsize=(10, 5.5))
# linefmt: 默认为grey,即灰实线
# 要将stem写在plot上面,否则stem的makerfmt会被看到
# 当化两个图时,注意np.arange()是从1开始
plt.stem(np.arange(1,4,1), a[0:3], linefmt=':', markerfmt ='v',  )
plt.plot(np.arange(1,4,1), a[0:3], color="red",marker = 'o', ms=10, linewidth=1,label = 'a',)

plt.stem(np.arange(4,8,1), a[3:], linefmt='grey', markerfmt ='o',  )
plt.plot(np.arange(4,8,1), a[3:], color="green",marker = 's', ms=10, linewidth=1,label = 'a',)
plt.legend()
plt.show()
View Code

 官网链接:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.stem.html#matplotlib.pyplot.stem

原文地址:https://www.cnblogs.com/xxswkl/p/10868672.html