matplotlib

very good tutorial
matplotlib和PIL打开图片的方法

command

  1. figure
  • size
    1
    2
    plt.figure(figsize=(20,10)) # this command shall be at beginning.
    <!-- more -->

Based on ref matplotlib.pyplot.figure, the default size of a plot is 6.4 and 4.8.
rcParams["figure.figsize"] = [6.4, 4.8]

  • subplots size
    fig, ax1 = plt.subplots(figsize=(20, 10))
    如果不用上面的命令,而用ax.figure.set_size_inches(50,10),不好使。

  • other thins
    plt.figure(dpi=80, facecolor='w', edgecolor='w')

  1. line
    plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-", label="balala data", marker='tri', markersize=3,)

  2. limit (range)
    plt.xlim(-4.0,4.0)

  3. legend
    plt.legend(loc='upper left')

  4. ticks
    plt.xticks(np.linspace(-4,4,9,endpoint=True))

  5. label of axis
    plt.xlabel("This is X axis")

  6. point style
    For style of point, see [1] and [2]

  7. save/load

    • save
      plt.savefig("name.png") Note that plt.show() will clean the content, so savefig should be in front
    • load
    1
    2
    img = mpimg.imread("3732video_star_distri.png")
    plt.imshow(img)
  8. histogram
    plt.hist(mydict.values(), bins=10)

  9. add additional ticks

    1
    2
    3
    4
    5
    6
    lines = plt.plot(x,y)
    ax = lines[0].axes
    lim = ax.get_ylim()
    ax.set_yticks(list(ax.get_yticks()) + extraticks_list)
    ax.set_ylim(lim)

ref: adding extra axis ticks using matplotlib

  1. arrow
    plt.arrow(datetime(2018,4,21), 1.268, 30, 0, linestyle=(0,(5,10)))
    注意:是(x,y,dx,dy),第三个和第四个参数是箭头的长度;这里30被当做30天;(offset,(onoffseq))格式的参考可见:linestyle

  2. add x2 axis
    ax2 = ax1.twinx()
    ref: example code


concept

global setting

1
2
mpl.rcParams['xtick.labelsize'] = 24
mpl.rcParams['ytick.labelsize'] = 24
  • font
    1
    2
    3
    4
    5
    font = {'family' : 'normal',
    'weight' : 'bold',
    'size' : 22}
    matplotlib.rc('font', **font)

ref:


  1. matplotlib.pyplot.plo

  2. marker example

谢谢~