效果展示:
源码示例:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 1.产生数据(步长0.1取点)
x = np.arange(-5,5,0.1)
y = np.sin(2*x)
# 2.创建一个窗口
ax1 = plt.subplot(1,1,1,facecolor='white')
# 3.创建一个窗口,多个子图(设置大小,分辨率)
# fig = plt.figure(num=1, figsize=(15, 8),dpi=80) #开启一个窗口,同时设置大小,分辨率
# ax1 = fig.add_subplot(2,1,1) #通过fig添加子图,参数:行数,列数,第几个。
# ax2 = fig.add_subplot(2,1,2)
# 4.设置窗口边距
# fig = plt.gcf()
# fig.subplots_adjust(left=0)
# 5.设置标题,大小
ax1.set_title('python-drawing',fontsize = 16)
# 6.设置横纵轴名称,大小
ax1.set_xlabel('x-name',fontsize = 14)
ax1.set_ylabel('y-name',fontsize = 14)
# 7.设置横纵轴刻度范围
ax1.set_xlim(-5,5)
ax1.set_ylim(-2.0,2.0)
# 8.自定义横纵轴刻度
# ax1.set_xticks([]) #去除坐标轴刻度
# ax1.set_xticks((-5,-3,-1,1,3,5)) #自定义坐标轴刻度
# ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5','x6'],rotation=-30,fontsize='small') #自定义添加文本
# 9.指定位置显示文字
ax1.text(1, 1.2, r'y=sin(2x)',fontsize = 12)
# 10.图像曲线添加注释(参数:注释文本、指向点、文字位置、箭头属性)
ax1.annotate('This is zero point', xy=(0, 0), xytext=(1, -1.5),arrowprops=dict(facecolor='black', shrink=0.05),fontsize = 12)
# 11.显示网格。which参数的值为major(只绘制大刻度)、minor(只绘制小刻度)
ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='gray',linestyle='--',linewidth=1)
# 12.图像局部放大(其实就是在当前画布重新画原始图像的一部分)rect=[左, 下, 宽, 高],是使用的绝对布局
axes1 = plt.axes([0.25, 0.2, 0.1, 0.1])
axes1.set_xlim(-1,1)
axes1.plot(x,y)
# 13.画曲线图(alpha表示透明度)
plot1=ax1.plot(x,y,marker='o',color='b',label='legend1')
plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2')
# 14.显示图例
ax1.legend()
# ax1.legend(loc='upper left')
# 15.显示图像
plt.show()