zoukankan      html  css  js  c++  java
  • matplotlib---设置坐标轴

    import matplotlib.pyplot as plt
    import numpy as np
    
    # 一维数组, 元素为从-3到3之间均匀地产生50个点
    x = np.linspace(-3, 3, 50)
    
    y1 = 2 * x + 1
    y2 = x ** 2
    
    
    # num=3表示图片上方标题, figsize设置figure大小
    plt.figure(num=3, figsize=(8, 5))
    plt.plot(x, y1)
    
    # 设置红色虚线宽度为1.0
    plt.plot(x, y2, color='red', linewidth=1, linestyle='--')
    
    # ## 设置坐标轴 ## #
    # 设置x和y轴的范围
    plt.xlim((-1, 2))
    plt.ylim((-2, 3))
    
    # 设置坐标轴含义, 中文需要加fontproperties属性
    plt.xlabel('价格', fontproperties='SimHei')
    plt.ylabel('利润', fontproperties='SimHei')
    
    # 设置x轴和y轴的刻度
    new_ticks = np.linspace(-1, 2, 5)
    plt.xticks(new_ticks)
    
    """
    设置对应坐标用汉字或英文表示, 后面的fontproperties表示中文不乱码,
    $$表示将英文括起来, r表示正则[配, 可以变为好看的字体
    显示特殊字符, 比如阿尔法, 用转义符alpha, 前面的表示空格转义
    """
    plt.yticks([-2, -1.8, -1, 1.22, 3.], ['非常糟糕', '糟糕', r'$good alpha$', r'$really good$', '超级好'],
               fontproperties='SimHei')
    
    # 设置边框/坐标轴
    # gca = 'get current axis/获取当前轴线'
    ax = plt.gca()
    # 取消右边和上边轴, spines就是四个边框
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    
    # matplotlib并没有设置默认的x轴和y轴, 设置默认轴
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
    
    # 设置坐标原点
    # 将(0, -1)设置为坐标原点
    # x轴放置到y轴-1点处
    ax.spines['bottom'].set_position(('data', -1))
    # 将y轴放置到x轴0点处
    ax.spines['left'].set_position(('data', 0))
    # 使用set_position之后中文显示不出来
    plt.yticks([-2, -1.8, -1, 1.22, 3.], ['非常糟糕', '糟糕', r'$good alpha$', r'$really good$', '超级好'],
               fontproperties='SimHei')
    
    plt.show()
    
    
  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/KX-Lau/p/13941453.html
Copyright © 2011-2022 走看看