import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm
# define data 数据设置
x = np.linspace(0.5,3.5,100)
y = np.sin(x)
y1 = np.random.randn(100)
# scatter figure 散点图绘制
plt.scatter(x,y1,c='0.25',label='scatter figure')
# plot figure 连续线条的绘制
plt.plot(x,y,ls='--',lw=2,label='plot figure')
# set x,yaxis limit x,y轴的范围设置
plt.xlim(0,4)
plt.ylim(-3,3)
# set axes labels 设置x,y轴的名称,前面两行是为了中文显示而写的
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.ylabel('y轴')
plt.xlabel('x轴')
# set x,yaxis grid 设置背景网格线
plt.grid(True,ls=':',color='r')
# add a horizontal line across the axis 参考线设置
plt.axhline(y=0,c='r',ls='--',lw=2)
# set a vertical span across the axis 参考区间设置
plt.axvspan(xmin=1.0,xmax=2.0,facecolor='y',alpha=0.3)
plt.axhs
python中使用函数matplotlib的图表组成元素代码实战及详细讲解
最新推荐文章于 2021-07-26 15:22:58 发布
本文通过实例代码介绍了Python中matplotlib库创建图表的基本元素,包括设置坐标轴范围、标签,绘制网格线、水平参考线、参考区域,添加注释文本、标题和图例,特别强调了中文标签的处理方法以及线条颜色和风格的选择。
摘要由CSDN通过智能技术生成