python-matplotlib绘图

import matplotlib.pyplot as plt
import numpy as np

def figure_test():
    # x轴范围
    x = np.linspace(-3, 5, 50)
    #y轴取值范围
    y = x**2-2*x-8
    # 设置figur名字与大小
    plt.figure(num='test', figsize=((5, 5)))
    #设置轴名称,若不设置fontproperties='SimHei'则中文显示乱码
    plt.xlabel(u'X-轴', fontproperties='SimHei')
    plt.ylabel(u'Y-轴', fontproperties='SimHei')
    #设置轴上刻度间隔,并显示间隔值
    # new_xticks = np.linspace(-3,5,9)
    # plt.xticks(new_xticks, ['','','','','中间值'], fontproperties='SimHei')
    # 绘制双曲线
    plt.plot(x, y, color='red', linewidth=1.0, linestyle='--')
    #获取默认坐标轴
    ax = plt.gca()
    #隐藏top与right坐标轴
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    # 将xy轴原点设置为0、0
    ax.spines['bottom'].set_position(('data',0))
    ax.spines['left'].set_position(('data', 0))

    #设置轴上刻度间隔,并显示间隔值
    new_xticks = np.linspace(-3,5,9)
    plt.xticks(new_xticks, ['','','','','中间值'], fontproperties='SimHei')

    plt.show()

运行效果图:

 

Annontation标注:
def annontation_test():
    x = np.linspace(-3, 3, 30)
    y = 2*x +1
    # 设置画布名称与大小
    plt.figure(num='one', figsize=((5,5)))
    plt.plot(x,y)
    # 绘制散点,s表示大小
    # plt.scatter(x,y,s=10, color='g')
    # 绘制垂直x轴的直线
    x2 = 2
    y2 = x2+5
    # 将两个点放入plot中,[x2,x2]表示x取值范围,[-y2,y2]表示y取值范围
    plt.plot([x2,x2], [-y2, y2], color='k', linewidth = 3)
    # 设置x/y轴取值范围
    plt.xlim(-3, 3)
    plt.ylim(-10, 10)

    # 获取四个坐标轴
    ax = plt.gca()
    # 隐藏上/右坐标轴
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    #将(0,0)设置为原点
    ax.spines['bottom'].set_position(('data', 0))
    ax.spines['left'].set_position(('data', 0))

    '''
    添加注释annotate
    其中参数xy为箭头将要指向的坐标点,xycoords='data'是说基于数据的值来选位置,
    xytext=(+10, -10)为文本相对与xy的位置,
    textcoord='offset points'表示基于相对位置,
    arrowprops是对图中箭头类型的一下设置
    '''
    plt.annotate(r'$2x+1=%s$'%y2, xy=(x2, 2*x2+1), xycoords='data', xytext=(+10, -10),
                 textcoords='offset points', fontsize=10,
                 arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=.2'))

    # 其中-3.7, 3,是选取text的位置, 空格需要用到转字符\ ,fontdict设置文本字体.
    plt.text(-3.7, 3, r'$This\ is\ the\ some\ text.\mu\ \sigma_i\ \alpha_t$',
             fontdict={'size': '16', 'color': 'red'})

    plt.show()

运行效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值