中文显示异常

  1. plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  2. plt.rcParams['font.serif'] = ['Microsoft YaHei']

坐标轴

设置XY坐标范围

  1. axes.set_xlim([xmin,xmax])
  2. axes.set_ylim([ymin,ymax])

设置最小刻度间隔

  1. axes_score.yaxis.set_minor_locator(MultipleLocator(2.5))
  2. axes_score.xaxis.set_minor_locator(MultipleLocator(0.5))

设置x轴时间刻度间隔

  1. axes_score.xaxis.set_minor_locator(mdate.MonthLocator()) #X轴时间间隔-月
  2. axes_score.xaxis.set_major_locator(mdate.HourLocator()) #X轴时间间隔-小时

设置(自定义)x轴时间标注间隔

  1. axes_score.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m')) # 设置x坐标格式
  2. plt.xticks(pd.date_range(start=date[0],end=date[-1],freq='5M'), # 自定义时间标注间隔,5M表示5个月
  3. rotation=90,fontproperties = 'Times New Roman',size=6) # 标签选择90度

显示效果
image.png
pd.date_range()参数
image.png

设置y轴的刻度

  1. axes.set_yticks([70, 75, 80, 85, 90, 95])

轴反向

  1. # x轴
  2. ax.xaxis.set_ticks_position('top') #将x轴的位置设置在顶部
  3. ax.invert_xaxis() #x轴反向
  4. # y轴
  5. ax.yaxis.set_ticks_position('right') #将y轴的位置设置在右边
  6. ax.invert_yaxis() #y轴反向

共享坐标

方法一

  1. # 共享坐标轴 方法一
  2. t = np.arange(0.01, 5.0, 0.01)
  3. s1 = np.sin(2 * np.pi * t)
  4. s2 = np.exp(-t)
  5. s3 = np.sin(4 * np.pi * t)
  6. plt.subplots_adjust(top=2) #位置调整
  7. ax1 = plt.subplot(311)
  8. plt.plot(t, s1)
  9. plt.setp(ax1.get_xticklabels(), fontsize=6)
  10. plt.title('我是原坐标')
  11. # 只共享X轴 sharex
  12. ax2 = plt.subplot(312, sharex=ax1)
  13. plt.plot(t, s2)
  14. # make these tick labels invisible
  15. plt.setp(ax2.get_xticklabels(), visible=False)
  16. plt.title('我共享了X轴')
  17. # 共享X轴和Y轴 sharex、sharey
  18. ax3 = plt.subplot(313, sharex=ax1, sharey=ax1)
  19. plt.plot(t, s3)
  20. plt.xlim(0.01, 5.0) #不起作用
  21. plt.title('我共享了X轴和Y轴')
  22. plt.show()

方法二

  1. # 共享坐标轴 方法二
  2. x = np.linspace(0, 2 * np.pi, 400)
  3. y = np.sin(x ** 2)
  4. f, axarr = plt.subplots(2, sharex=True)
  5. f.suptitle('共享X轴')
  6. axarr[0].plot(x, y)
  7. axarr[1].scatter(x, y, color='r')
  8. f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
  9. f.suptitle('共享Y轴')
  10. ax1.plot(x, y)
  11. ax2.scatter(x, y)
  12. f, axarr = plt.subplots(3, sharex=True, sharey=True)
  13. f.suptitle('同时共享X轴和Y轴')
  14. axarr[0].plot(x, y)
  15. axarr[1].scatter(x, y)
  16. axarr[2].scatter(x, 2 * y ** 2 - 1, color='g')
  17. # 间距调整为0
  18. f.subplots_adjust(hspace=0)
  19. # 设置全部标签在外部
  20. for ax in axarr:
  21. ax.label_outer()

隐藏刻度线、标签和边线

隐藏所有刻度线和标签

  1. plt.axis('off')
  2. ax.axis('off')

效果
image.png

隐藏刻度线和标签

  1. plt.xticks([])
  2. plt.yticks([])
  3. ax.set_xticks([])
  4. ax.set_yticks([])

隐藏刻度线

  1. plt.tick_params(bottom=False, top=False, left=False, right=False)
  2. ax.tick_params(bottom=False, top=False, left=False, right=False)

隐藏边线

  1. for i in ['top', 'right', 'bottom', 'left']:
  2. ax.spines[i].set_visible(False)

轴标签旋转加字体设置

  1. plt.xticks(x,rotation=90,fontproperties = 'Times New Roman',size=6) #标签选择90度,x表示画图x坐标,所有点显示
  2. plt.yticks(fontproperties = 'Times New Roman',size=10) #y标签

设置x、y轴标题

  1. axes.set_ylabel("Generation Consistency")
  2. axes.set_xlabel("KB Row Number")

添加图例

  1. axes.legend()

画网格线

  1. axes.grid(True) # 全部
  2. ax.grid(axis='y') # 绘制y轴方向网格线条
  3. ax.grid(axis='x') # 绘制x轴方向网格线条

添加数据标签

方法一

  1. for x_, y_ in zip(x, y):
  2. axes.text(x_, y_, y_, ha='left', va='bottom', fontsize=10.5)
  3. for x_, y_ in zip(x, y2):
  4. axes.text(x_, y_, y_, ha='left', va='bottom', fontsize=10.5)

方法二
逐个获取需要标注的点的横纵坐标 x与 y,然后在位置 (x, y+0.3) 处以 10.5 的字体显示出 y 的值,‘center’ 和 ‘bottom’ 分别指水平和垂直方向上的对齐方式。

  1. for x, y in zip(xList, yList):
  2. plt.text(x, y+0.3, '%.0f'%y, ha='center', va='bottom', fontsize=10.5)