美文网首页
Matplotlib之基本点线作图

Matplotlib之基本点线作图

作者: 哀思希哀 | 来源:发表于2017-10-07 15:02 被阅读0次

    在Jupyter Notebook下实现各种Matplotlib用法。

    #导入库和数据
    >>>import matplotlib.pyplot as plt
    import matplotlib
    import numpy as np
    %matplotlib inline
    
    x = np.linspace(-1, 1, 20)
    y1 = 2*x + 1
    y2 = x**2
    y3 = np.zeros(20)+0.5
    

    基本作图

    plt.figure(num=1, figsize=(10,6))#定义一个图像窗口,编号为1,大小(10,6)
    plt.plot(x, y1)#画默认线
    plt.plot(x, y2, color='red', linewidth=5.0, linestyle='-.')#设置颜色,线宽,线的样式
    plt.plot(x,y3,'o',color='green',markersize=10)#画点,设置点的样式,颜色,大小
    plt.title('Matplotlib Test',fontsize=50)
    
    Figure 1.png

    坐标轴操作

    plt.figure(num=2, figsize=(10,6))
    
    #坐标刻度范围
    plt.xlim((-2, 2))
    plt.ylim((-3, 5))
    
    #坐标标签
    plt.xlabel('I am x',fontsize=20)#设置坐标标签和大小
    plt.ylabel('I am y')
    
    #坐标刻度间隔
    new_ticks = np.linspace(-2, 2, 15)
    plt.xticks(new_ticks)#括号为空即不显示刻度
    #设置刻度名称
    plt.yticks([-2.5, -1.8, -1, 1.22, 3],['really bad', 'bad', 'normal', 'good', 'really good'])
    #设置刻度位置
    ax = plt.gca()#获取坐标轴
    ax.xaxis.set_ticks_position('top')#(所有位置:left,right,both,default,none)
    #设置刻度方向
    matplotlib.rcParams['xtick.direction'] = 'inout' #in,out,inout三种属性
    matplotlib.rcParams['ytick.direction'] = 'inout' #in,out,inout三种属性
    
    #设置坐标轴位置和颜色
    ax = plt.gca()#获取坐标轴
    ax.spines['right'].set_color('none')#使用.spines获取边框,设置右边无色
    ax.spines['top'].set_color('r')#使用.spines获取边框,设置上边红色
    ax.spines['bottom'].set_position(('data', 0))#使用.spines获取边框,设置底边位置为data位,x=0,(位置所有属性:outward,axes,data)
    ax.spines['left'].set_position(('data',-0.5))#使用.spines获取边框,设置左边位置为data位,y=0.5,(位置所有属性:outward,axes,data)
    
    Figure 2.png

    图例操作

    """
    注:valid locations are best, upper right, upper left, lower left, lower right,
    right, center left, center right, lower center, upper center, center
    """
    plt.figure(num=3, figsize=(10,6))
    l1, = plt.plot(x, y2, label='linear line')#画图,设置图例名称
    l2, = plt.plot(x, y1, color='red', linewidth=5.0, linestyle='--', label='square line')
    plt.legend(loc='lower left')
    plt.legend(handles=[l1, l2], labels=['up', 'down'],  loc='best', fontsize=30)
    
    Figure 3.png

    注释操作

    plt.figure(num=4, figsize=(10, 6))
    #画图并设置坐标轴
    plt.plot(x, y1,'-', linewidth=15, zorder=1)
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_position(('data', 0))
    ax.spines['left'].set_position(('data', 0))
    
    x0 = 0.25
    y0 = 2*x0 + 1
    plt.plot([x0, x0,], [0, y0,], 'k--', linewidth=2.5)
    #画出特定点
    plt.scatter([x0, ], [y0, ], s=500, color='r', zorder=2)
    #zorder=1,2...从下至上表示图层顺序, plt.lengend()也有set_zorder(20)方法
    
    #添加标注
    plt.annotate('2x+1=%s' % y0, xy=(x0, y0), xycoords='data', xytext=(+300, -30),
                 textcoords='offset points', fontsize=20,
                 arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
    
    #添加文字注释
    plt.text(-2, 1.5, r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$', 
             fontdict={'size': 25, 'color': 'm'})
    
    Figure 4.png

    其它

    plt.figure(num=5, figsize=(10, 6))
    ax=plt.gca()
    #设置刻度名称的颜色,透明度,使用set_bbox()方法,alpha表示透明度。
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontsize(12)
        label.set_bbox(dict(facecolor='red', edgecolor='k', alpha=0.5))
    
    Figure 5.png

    相关文章

      网友评论

          本文标题:Matplotlib之基本点线作图

          本文链接:https://www.haomeiwen.com/subject/hbyjyxtx.html