Matplotlib知识点

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.rand(25,2), columns=['value1', 'value2'])
fig = df.plot(figsize=(20, 8), linestyle='-.', marker='o', color='r') # 线形和点的参数在下方
plt.title('Just For Exercise') # 设置图表的标题
plt.xlabel('Xnum') # 设置X轴的标签
plt.ylabel('Ynum') # 设置Y轴的标签
plt.legend(loc=1) # 设置图例,loc参数是位置,具体内容在下方
plt.xlim([0, 20]) # 设置X轴的边界范围
plt.ylim([0, 1.5]) # 设置Y轴的边界范围
plt.xticks(range(18)) # 设置X刻度
plt.yticks([0,0.2,0.4,0.6,0.8,1.0,1.2]) # 设置Y刻度
fig.set_xticklabels("%.1f" %i for i in range(18)) # X轴刻度标签
fig.set_yticklabels("%.2f" %i for i in [0,0.2,0.4,0.6,0.8,1.0,1.2]) # Y轴刻度标签
plt.grid() # 创建格网,参数在下方
plt.savefig('路径位置', dpi=200, facecolor='g', edgecolor='b')
  • jupyter notebook的魔法方法
    • %matplotlib inline相当于plt.show() %matplotlib notebook会出来一个交互图层

  • legend(loc=),显示图例,loc代表图例在图中的位置
    • # 'best' : 0,自动识别一个位置
      # 'upper right' : 1,
      # 'upper left' : 2,
      # 'lower left' : 3,
      # 'lower right' : 4,
      # 'right' : 5,
      # 'center left' : 6,
      # 'center right' : 7,
      # 'lower center' : 8,
      # 'upper center' : 9,
      # 'center' : 10,

  • grid(),添加格网
    • plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')
    • 显示网格
    • linestyle:线型
    • color:颜色
    • linewidth:宽度
    • axis:x,y,both,显示x/y/两者的格网

  • linestyle线形
    • '-'   solid line style
    • '--'  dashed line style 
    • '-.'  dash-dot line style
    • ':'   dotted line style

  • marker点
    • '.' point marker
      ',' pixel marker
      'o' circle marker
      'v' triangle_down marker
      '^' triangle_up marker
      '<' triangle_left marker
      '>' triangle_right marker
      '1' tri_down marker
      '2' tri_up marker
      '3' tri_left marker
      '4' tri_right marker
      's' square marker
      'p' pentagon marker
      '*' star marker
      'h' hexagon1 marker
      'H' hexagon2 marker
      '+' plus marker
      'x' x marker
      'D' diamond marker
      'd' thin_diamond marker
      '|' vline marker
      '_' hline marker

plt.gcf().clear() 每次清空图表内内容
posted @ 2021-09-22 22:39  ArunC  阅读(71)  评论(0)    收藏  举报