• matplotlib基础加进阶


    一. 一幅图带你了解matplotlib

    1.在matplotlib中绘制折线图

    2.改变图形大小

    3.设置x轴和y轴的坐标轴刻度范围

    4.展示网格

    5.保存文件到本地

    6. 绘制带有标记的折线图

    7.添加带有数学公式图例

    8.不显示坐标轴

    9.设置字体大小,字体为斜体或者加粗

    #使用无衬线字体arial,字体大小为12,并加粗,变为斜体,这里是个字典形式
    fontparams={
        "font.size":12,
        "font.weight":'bold',
        "font.family":"arial"  
    }
    labelparams={
        "size":20,
        "weight":"semibold",#这个是半粗体
        "family":"serif",
        "style":"italic"
    }
    plt.rcParams.update(fontparams)
    #首先设置x轴和y轴的数据
    data_x=[i for i in range(-6,7)]
    data_y=[j*j for j in data_x]
    #改变图形大小为10:6,change the size of figure
    plt.rcParams["figure.figsize"]=(10,6)
    #这里就是添加标记,改变默认的设置,注意这里是中括号[]不是()
    plt.rc("lines",linewidth=2,linestyle="-",marker="*")
    plt.rcParams["lines.markersize"]=15
    plt.rcParams["font.size"]="10.0"
    #改变刻度范围
    plt.xlim(-6,6)
    plt.ylim(0,36)
    #设置坐标轴和标题,set the label of x and y
    plt.title("Interactive Plot",labelparams)
    plt.xlabel("X-axis",labelparams)
    plt.ylabel("Y-axis",labelparams)
    #添加网格,并设置网格线的大小,颜色,线条类型
    plt.grid(True,color="blue",linewidth=0.5,linestyle="-")
    #不显示坐标轴
    # plt.axis("off"),如果你想用的话那就取消注释
    #添加图例
    label="this is a math function:$y=x^2$"
    #画图
    plt.plot(data_x,data_y,label=label)
    plt.legend(loc="upper right")#这里的loc设置图例在右上方
    plt.show()
    #保存图片,这一步要把上面的plt.show()给注释掉,防止保存为空白文件
    plt.savefig(r"C:/Users/kjl/Desktop/demo.jpg")
    ##也可以保存为pdf文件,同时设置高dpi(dots per inch)来保持清晰度,并使用bbox_inches="tight"删除了图形周围的所有多余空白
    plt.savefig(r"C:/Users/kjl/Desktop/demo.pdf",dpi=1200,format="pdf",bbox_inches="tight")
    # 效果如下所示
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44


    output_3_0

    pdf如下:

    image-20221101172535407

    二.颜色的多种设置

    plt.title("Interactive Plot")
    for i in range(0,10):
        data_x=[i for i in range(-i,i)]
        color=f"C{i}"
        plt.plot(data_x,c=color)
    plt.show
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    output_8_1

    三. 查看可用字体

    import matplotlib.pyplot as plt
    from matplotlib import font_manager
     
    for font in font_manager.fontManager.ttflist:
        # 查看字体名以及对应的字体文件名
        print(font.name, '-', font.fname)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    STIXNonUnicode - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXNonUni.ttf
    STIXSizeOneSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizOneSymReg.ttf
    cmsy10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmsy10.ttf
    STIXNonUnicode - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXNonUniBol.ttf
    STIXSizeTwoSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizTwoSymBol.ttf
    DejaVu Sans Mono - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSansMono.ttf
    DejaVu Serif - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSerif-BoldItalic.ttf
    DejaVu Sans - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSans.ttf
    DejaVu Serif - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSerif-Italic.ttf
    STIXGeneral - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXGeneral.ttf
    cmb10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmb10.ttf
    cmr10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmr10.ttf
    DejaVu Sans Display - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSansDisplay.ttf
    STIXSizeThreeSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizThreeSymReg.ttf
    DejaVu Serif Display - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSerifDisplay.ttf
    STIXGeneral - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXGeneralItalic.ttf
    STIXSizeThreeSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizThreeSymBol.ttf
    DejaVu Sans - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSans-BoldOblique.ttf
    DejaVu Sans - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSans-Oblique.ttf
    STIXSizeFourSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizFourSymReg.ttf
    STIXSizeTwoSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizTwoSymReg.ttf
    STIXGeneral - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXGeneralBolIta.ttf
    DejaVu Sans Mono - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSansMono-Bold.ttf
    cmex10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmex10.ttf
    DejaVu Sans Mono - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSansMono-Oblique.ttf
    STIXGeneral - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXGeneralBol.ttf
    cmtt10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmtt10.ttf
    STIXSizeFourSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizFourSymBol.ttf
    cmss10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmss10.ttf
    DejaVu Serif - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSerif-Bold.ttf
    DejaVu Sans - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSans-Bold.ttf
    STIXNonUnicode - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXNonUniBolIta.ttf
    cmmi10 - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\cmmi10.ttf
    DejaVu Serif - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSerif.ttf
    DejaVu Sans Mono - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSansMono-BoldOblique.ttf
    STIXSizeFiveSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizFiveSymReg.ttf
    STIXSizeOneSym - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXSizOneSymBol.ttf
    STIXNonUnicode - D:\anaconda\lib\site-packages\matplotlib\mpl-data\fonts\ttf\STIXNonUniIta.ttf
    Cambria - C:\Windows\Fonts\cambriai.ttf
    Cambria - C:\Windows\Fonts\cambriaz.ttf
    Yu Gothic - C:\Windows\Fonts\YuGothR.ttc
    Gadugi - C:\Windows\Fonts\gadugib.ttf
    Corbel - C:\Windows\Fonts\corbell.ttf
    Microsoft PhagsPa - C:\Windows\Fonts\phagspab.ttf
    Malgun Gothic - C:\Windows\Fonts\malgunsl.ttf
    Calibri - C:\Windows\Fonts\calibrib.ttf
    Marlett - C:\Windows\Fonts\marlett.ttf
    Microsoft PhagsPa - C:\Windows\Fonts\phagspa.ttf
    Microsoft YaHei - C:\Windows\Fonts\msyh.ttc
    Courier New - C:\Windows\Fonts\courbi.ttf
    Microsoft Tai Le - C:\Windows\Fonts\taileb.ttf
    Georgia - C:\Windows\Fonts\georgia.ttf
    Javanese Text - C:\Windows\Fonts\javatext.ttf
    Arial - C:\Windows\Fonts\ariblk.ttf
    Nirmala UI - C:\Windows\Fonts\Nirmala.ttf
    Corbel - C:\Windows\Fonts\corbelz.ttf
    Calibri - C:\Windows\Fonts\calibrili.ttf
    Candara - C:\Windows\Fonts\Candarai.ttf
    Leelawadee UI - C:\Windows\Fonts\LeelUIsl.ttf
    Trebuchet MS - C:\Windows\Fonts\trebucit.ttf
    Microsoft Tai Le - C:\Windows\Fonts\taile.ttf
    Nirmala UI - C:\Windows\Fonts\NirmalaB.ttf
    Constantia - C:\Windows\Fonts\constan.ttf
    Comic Sans MS - C:\Windows\Fonts\comici.ttf
    Candara - C:\Windows\Fonts\Candarab.ttf
    Microsoft Himalaya - C:\Windows\Fonts\himalaya.ttf
    FangSong - C:\Windows\Fonts\simfang.ttf
    Calibri - C:\Windows\Fonts\calibril.ttf
    Calibri - C:\Windows\Fonts\calibriz.ttf
    Segoe UI - C:\Windows\Fonts\segoeuisl.ttf
    Sitka - C:\Windows\Fonts\SitkaVF-Italic.ttf
    Nirmala UI - C:\Windows\Fonts\NirmalaS.ttf
    Sylfaen - C:\Windows\Fonts\sylfaen.ttf
    Candara - C:\Windows\Fonts\Candarali.ttf
    Myanmar Text - C:\Windows\Fonts\mmrtextb.ttf
    Consolas - C:\Windows\Fonts\consolab.ttf
    Corbel - C:\Windows\Fonts\corbeli.ttf
    Tahoma - C:\Windows\Fonts\tahomabd.ttf
    Ink Free - C:\Windows\Fonts\Inkfree.ttf
    Palatino Linotype - C:\Windows\Fonts\palabi.ttf
    Lucida Console - C:\Windows\Fonts\lucon.ttf
    Times New Roman - C:\Windows\Fonts\timesbi.ttf
    Comic Sans MS - C:\Windows\Fonts\comicz.ttf
    KaiTi - C:\Windows\Fonts\simkai.ttf
    Verdana - C:\Windows\Fonts\verdanaz.ttf
    Segoe UI - C:\Windows\Fonts\seguibl.ttf
    Malgun Gothic - C:\Windows\Fonts\malgun.ttf
    Courier New - C:\Windows\Fonts\cour.ttf
    Georgia - C:\Windows\Fonts\georgiab.ttf
    Arial - C:\Windows\Fonts\ariali.ttf
    Trebuchet MS - C:\Windows\Fonts\trebucbd.ttf
    Microsoft New Tai Lue - C:\Windows\Fonts\ntailub.ttf
    Segoe UI - C:\Windows\Fonts\seguisb.ttf
    Bahnschrift - C:\Windows\Fonts\bahnschrift.ttf
    Yu Gothic - C:\Windows\Fonts\YuGothL.ttc
    Ebrima - C:\Windows\Fonts\ebrimabd.ttf
    Corbel - C:\Windows\Fonts\corbel.ttf
    Calibri - C:\Windows\Fonts\calibrii.ttf
    Verdana - C:\Windows\Fonts\verdana.ttf
    Verdana - C:\Windows\Fonts\verdanai.ttf
    MV Boli - C:\Windows\Fonts\mvboli.ttf
    Segoe UI - C:\Windows\Fonts\seguibli.ttf
    Segoe UI - C:\Windows\Fonts\segoeuil.ttf
    Leelawadee UI - C:\Windows\Fonts\LeelaUIb.ttf
    HoloLens MDL2 Assets - C:\Windows\Fonts\holomdl2.ttf
    Comic Sans MS - C:\Windows\Fonts\comic.ttf
    Corbel - C:\Windows\Fonts\corbelli.ttf
    Microsoft New Tai Lue - C:\Windows\Fonts\ntailu.ttf
    Constantia - C:\Windows\Fonts\constani.ttf
    Segoe UI Historic - C:\Windows\Fonts\seguihis.ttf
    Microsoft Sans Serif - C:\Windows\Fonts\micross.ttf
    Lucida Sans Unicode - C:\Windows\Fonts\l_10646.ttf
    Franklin Gothic Medium - C:\Windows\Fonts\framd.ttf
    DengXian - C:\Windows\Fonts\Dengb.ttf
    Georgia - C:\Windows\Fonts\georgiaz.ttf
    Segoe UI - C:\Windows\Fonts\seguisli.ttf
    Tahoma - C:\Windows\Fonts\tahoma.ttf
    Segoe UI Variable - C:\Windows\Fonts\SegUIVar.ttf
    Arial - C:\Windows\Fonts\arialbd.ttf
    Constantia - C:\Windows\Fonts\constanz.ttf
    Palatino Linotype - C:\Windows\Fonts\palab.ttf
    Segoe UI - C:\Windows\Fonts\seguisbi.ttf
    Consolas - C:\Windows\Fonts\consola.ttf
    SimSun - C:\Windows\Fonts\simsun.ttc
    Microsoft YaHei - C:\Windows\Fonts\msyhl.ttc
    Candara - C:\Windows\Fonts\Candara.ttf
    Georgia - C:\Windows\Fonts\georgiai.ttf
    Trebuchet MS - C:\Windows\Fonts\trebucbi.ttf
    Microsoft JhengHei - C:\Windows\Fonts\msjh.ttc
    Candara - C:\Windows\Fonts\Candaral.ttf
    DengXian - C:\Windows\Fonts\Dengl.ttf
    DengXian - C:\Windows\Fonts\Deng.ttf
    Consolas - C:\Windows\Fonts\consolaz.ttf
    Palatino Linotype - C:\Windows\Fonts\pala.ttf
    Arial - C:\Windows\Fonts\arialbi.ttf
    Verdana - C:\Windows\Fonts\verdanab.ttf
    Sitka - C:\Windows\Fonts\SitkaVF.ttf
    Segoe MDL2 Assets - C:\Windows\Fonts\segmdl2.ttf
    Trebuchet MS - C:\Windows\Fonts\trebuc.ttf
    Consolas - C:\Windows\Fonts\consolai.ttf
    Courier New - C:\Windows\Fonts\courbd.ttf
    Segoe Print - C:\Windows\Fonts\segoepr.ttf
    Cambria - C:\Windows\Fonts\cambriab.ttf
    Segoe Fluent Icons - C:\Windows\Fonts\SegoeIcons.ttf
    MS Gothic - C:\Windows\Fonts\msgothic.ttc
    Microsoft Yi Baiti - C:\Windows\Fonts\msyi.ttf
    Segoe Script - C:\Windows\Fonts\segoescb.ttf
    SimSun-ExtB - C:\Windows\Fonts\simsunb.ttf
    Impact - C:\Windows\Fonts\impact.ttf
    Comic Sans MS - C:\Windows\Fonts\comicbd.ttf
    Microsoft JhengHei - C:\Windows\Fonts\msjhl.ttc
    Microsoft JhengHei - C:\Windows\Fonts\msjhbd.ttc
    Corbel - C:\Windows\Fonts\corbelb.ttf
    Leelawadee UI - C:\Windows\Fonts\LeelawUI.ttf
    Arial - C:\Windows\Fonts\arial.ttf
    Segoe UI - C:\Windows\Fonts\segoeuii.ttf
    SimHei - C:\Windows\Fonts\simhei.ttf
    Courier New - C:\Windows\Fonts\couri.ttf
    Palatino Linotype - C:\Windows\Fonts\palai.ttf
    Microsoft YaHei - C:\Windows\Fonts\msyhbd.ttc
    Segoe UI Symbol - C:\Windows\Fonts\seguisym.ttf
    Myanmar Text - C:\Windows\Fonts\mmrtext.ttf
    Wingdings - C:\Windows\Fonts\wingding.ttf
    Candara - C:\Windows\Fonts\Candaraz.ttf
    Malgun Gothic - C:\Windows\Fonts\malgunbd.ttf
    Gabriola - C:\Windows\Fonts\Gabriola.ttf
    Times New Roman - C:\Windows\Fonts\timesbd.ttf
    Franklin Gothic Medium - C:\Windows\Fonts\framdit.ttf
    MingLiU-ExtB - C:\Windows\Fonts\mingliub.ttc
    Times New Roman - C:\Windows\Fonts\timesi.ttf
    Segoe UI - C:\Windows\Fonts\segoeuib.ttf
    Times New Roman - C:\Windows\Fonts\times.ttf
    Segoe UI - C:\Windows\Fonts\segoeuiz.ttf
    Yu Gothic - C:\Windows\Fonts\YuGothM.ttc
    Ebrima - C:\Windows\Fonts\ebrima.ttf
    Webdings - C:\Windows\Fonts\webdings.ttf
    Segoe Print - C:\Windows\Fonts\segoeprb.ttf
    Segoe Script - C:\Windows\Fonts\segoesc.ttf
    Segoe UI - C:\Windows\Fonts\seguili.ttf
    Gadugi - C:\Windows\Fonts\gadugi.ttf
    Mongolian Baiti - C:\Windows\Fonts\monbaiti.ttf
    Constantia - C:\Windows\Fonts\constanb.ttf
    Segoe UI Emoji - C:\Windows\Fonts\seguiemj.ttf
    Cambria - C:\Windows\Fonts\cambria.ttc
    Calibri - C:\Windows\Fonts\calibri.ttf
    Segoe UI - C:\Windows\Fonts\segoeui.ttf
    Sans Serif Collection - C:\Windows\Fonts\SansSerifCollection.ttf
    Yu Gothic - C:\Windows\Fonts\YuGothB.ttc
    Symbol - C:\Windows\Fonts\symbol.ttf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189

    四. 以随机坐标绘制所有可用标记

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.lines import Line2D
    #准备50个随机数来画图
    a,b=np.random.rand(50),np.random.rand(50)
    markindex=np.random.randint(0,len(Line2D.markers),50)
    for x,y in enumerate(Line2D.markers):
        plt.scatter(a[x],b[x],marker=y)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    output_12_0

    参考链接:又再肝3天,整理了65个Matplotlib案例,这能不收藏? (qq.com)

    看完这篇文章受益良多

  • 相关阅读:
    Ansys Speos | 新型计算方法:使用 GPU 提升计算速率
    Git报错:git@github.com: Permission denied (publickey)
    Linux密码遗忘?别慌!解锁你的系统小秘籍!
    Spark之【基础介绍】
    jvm 一之 类加载器
    如何让JOIN跑得更快
    Linux自用笔记
    Rust开发——Vec向量
    Synchronized 与 Lock 的使用
    python提效小工具-统计xmind用例数量
  • 原文地址:https://blog.csdn.net/qq_54423921/article/details/127637792