zoukankan      html  css  js  c++  java
  • 4.10Python数据处理篇之Matplotlib系列(十)---文本的显示

    目录

    前言

    今天我们来学习一下文本的显示

    (一)中文显示

    1.全局的设置

    (1)说明:

    在matplotlib目前的绘图文字显示时,是不支持中文的,我们想输出中文,需要设置一下。

    matplotlib.rcParams['属性'] = '属性值' ,可以修改全局字体

    字体属性 'font.family'
    黑体 'SimHei'
    楷体 'Kaiti'
    隶书 'LiSu'
    仿宋 'FangSong'
    幼圆 'YouYuan'
    华文宋体 'STSong'
    字体格式 'font.style'
    正常 'normal'
    斜体 'italic'
    字体的大小'font.size'
    字号
    大号 large
    大小号 x-small

    (2)源代码

    import matplotlib.pyplot as plt
    import matplotlib
    
    # 将全局的字体设置为黑体
    matplotlib.rcParams['font.family'] = 'SimHei'
    
    y = [3, 1, 4, 5, 2]
    plt.plot(y)
    plt.ylabel("纵轴的值")
    plt.xlabel("横轴的值")
    
    # 自动保存图片
    plt.savefig("test", dpi=600)
    plt.show()
    

    (3)输出效果

    01.png

    2.局部的设置

    (1)说明:

    为了不影响全局的字体,我们可以选择在局部改变字体。

    在需要输入中文的地方,输入一下参数

    字体 fontproperties="SimHei"
    字号 fontsize=20
    颜色 color="green"

    (2)源代码

    import matplotlib.pyplot as plt
    
    y = [3, 1, 4, 5, 2]
    plt.plot(y)
    # 改变局部变量
    plt.ylabel("纵轴的值", fontproperties="SimHei", fontsize=20)
    plt.xlabel("横轴的值", fontproperties="SimHei", fontsize=20, color="green")
    plt.savefig("test", dpi=600)
    plt.show()
    

    (3)输出效果

    02.png

    (二)文本显示

    (1)说明:

    x轴标签 plt.xlabel("string")
    y轴标签 plt.ylabel("string")
    整体的标签 plt.titile("string")
    任意的位置 plt.text(x, y, "string")
    带箭头 plt.annotate(s, xy=(x, y), xytext=(x, y),arrowprops)
    $$ 数学公式

    带箭头的参数:

    s : "string"

    xy: 箭头的坐标

    xytext: 文字的坐标

    arrowprops: 箭头的属性,字典类型

    arrowprops=dict(facecolor="red", shrink=0.1, width=2)

    facecolor:箭头颜色

    shrink:箭头的长度(两坐标距离的比例,0~1)

    箭头的宽度

    (2)源代码

    import matplotlib.pyplot as plt
    
    y = [3, 1, 4, 5, 2]
    plt.plot(y)
    
    # x , y 轴标签
    plt.ylabel("纵轴的值", fontproperties="SimHei", fontsize=20)
    plt.xlabel("横轴的值", fontproperties="SimHei", fontsize=20, color="green")
    
    # 整体的标签
    plt.title(r"整体的标签 $x^{2y +3}$", fontproperties="SimHei", fontsize=30)
    
    # 显示网格
    plt.grid(True)
    
    # 再坐标为(1,3)处输出文字
    plt.text(1, 3, r"$mu=100$")
    
    # 有箭头的文字
    plt.annotate(r"$sum_1^nx$", xy=(3, 3), xytext=(3, 4.5),
                 arrowprops=dict(facecolor="red", shrink=0.1, width=2))
    
    # 设置坐标轴 x(0, 4) y(0, 6)
    plt.axis([0, 4, 0, 6])
    plt.show()
    

    (3)输出效果

    03.png

    作者:Mark

    日期:2019/02/06 周三

  • 相关阅读:
    OI无关--关于侧边栏
    Codeforces Round #528 div1
    BZOJ 3531: [Sdoi2014]旅行
    BZOJ 4538: [Hnoi2016]网络
    Codeforces Round #527 (Div. 3)
    Avito Cool Challenge 2018
    Educational Codeforces Round 56 (Rated for Div. 2)
    Codeforces Round #526 (Div. 1)
    2018-2019 Russia Open High School Programming Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
    Codeforces Round #525 (Div. 2)
  • 原文地址:https://www.cnblogs.com/zyg123/p/10517835.html
Copyright © 2011-2022 走看看