1.导包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
2.运行时配置参数
- rcParams : runtime configuration Parameters
- 运行时配置参数
%matplotlib inline
plt.rcParams['font.sans-serif'] = "SimHei"
plt.rcParams["axes.unicode_minus"] = False
%config Inlinebackend.figure_format = "svg"
from matplotlib.font_manager import FontManager
fm = FontManager()
my_fonts = set(f.name for f in fm.ttflist)
my_fonts
{'Agency FB',
'Algerian',
'Arial',
'Arial Rounded MT Bold',
'Arvo',
'Bahnschrift',
'Baskerville Old Face',
'Bauhaus 93',
'Bell MT',
'Berlin Sans FB',
'Berlin Sans FB Demi',
'Bernard MT Condensed',
'Blackadder ITC',
'Bodoni MT',
'Book Antiqua',
'Bookman Old Style',
'Bookshelf Symbol 7',
'Bradley Hand ITC',
'Britannic Bold',
'Broadway',
'Brush Script MT',
'Calibri',
'Californian FB',
'Calisto MT',
'Cambria',
'Candara',
'Castellar',
'Centaur',
'Century',
'Century Gothic',
'Century Schoolbook',
'Chiller',
'Colonna MT',
'Comic Sans MS',
'Consolas',
'Constantia',
'Cooper Black',
'Copperplate Gothic Bold',
'Copperplate Gothic Light',
'Corbel',
'Courier New',
'Curlz MT',
'DejaVu Math TeX Gyre',
'DejaVu Sans',
'DejaVu Sans Display',
'DejaVu Sans Mono',
'DejaVu Serif',
'DejaVu Serif Display',
'DengXian',
'Droid Serif',
'Dubai',
'Ebrima',
'Edwardian Script ITC',
'Elephant',
'Engravers MT',
'Eras Bold ITC',
'Eras Demi ITC',
'Eras Light ITC',
'Eras Medium ITC',
'FZShuTi',
'FZYaoTi',
'FangSong',
'Felix Titling',
'Footlight MT Light',
'Forte',
'Franklin Gothic Book',
'Franklin Gothic Demi',
'Franklin Gothic Demi Cond',
'Franklin Gothic Heavy',
'Franklin Gothic Medium',
'Franklin Gothic Medium Cond',
'Freestyle Script',
'French Script MT',
'Gabriola',
'Gadugi',
'Garamond',
'Georgia',
'Gigi',
'Gill Sans MT',
'Gill Sans MT Condensed',
'Gill Sans MT Ext Condensed Bold',
'Gill Sans Ultra Bold',
'Gill Sans Ultra Bold Condensed',
'Gloucester MT Extra Condensed',
'Goudy Old Style',
'Goudy Stout',
'Haettenschweiler',
'Harlow Solid Italic',
'Harrington',
'High Tower Text',
'HoloLens MDL2 Assets',
'IQYHT',
'Impact',
'Imprint MT Shadow',
'Indie Flower',
'Informal Roman',
'Ink Free',
'Javanese Text',
'Jokerman',
'Juice ITC',
'KaiTi',
'Kristen ITC',
'Kunstler Script',
'Leelawadee',
'Leelawadee UI',
'LiSu',
'Lobster',
'Lucida Bright',
'Lucida Calligraphy',
'Lucida Console',
'Lucida Fax',
'Lucida Handwriting',
'Lucida Sans',
'Lucida Sans Typewriter',
'Lucida Sans Unicode',
'MS Gothic',
'MS Outlook',
'MS Reference Sans Serif',
'MS Reference Specialty',
'MT Extra',
'MV Boli',
'Magneto',
'Maiandra GD',
'Malgun Gothic',
'Matura MT Script Capitals',
'Microsoft Himalaya',
'Microsoft JhengHei',
'Microsoft New Tai Lue',
'Microsoft PhagsPa',
'Microsoft Sans Serif',
'Microsoft Tai Le',
'Microsoft Uighur',
'Microsoft YaHei',
'Microsoft Yi Baiti',
'MingLiU-ExtB',
'Mistral',
'Modern No. 20',
'Mongolian Baiti',
'Monotype Corsiva',
'Myanmar Text',
'Niagara Engraved',
'Niagara Solid',
'Nirmala UI',
'OCR A Extended',
'Old English Text MT',
'Onyx',
'Open Sans',
'Palace Script MT',
'Palatino Linotype',
'Papyrus',
'Parchment',
'Perpetua',
'Perpetua Titling MT',
'Playbill',
'Poiret One',
'Poor Richard',
'Pristina',
'Rage Italic',
'Raleway',
'Ravie',
'Roboto',
'Roboto Condensed',
'Roboto Slab',
'Rockwell',
'Rockwell Condensed',
'Rockwell Extra Bold',
'STCaiyun',
'STFangsong',
'STHupo',
'STIXGeneral',
'STIXNonUnicode',
'STIXSizeFiveSym',
'STIXSizeFourSym',
'STIXSizeOneSym',
'STIXSizeThreeSym',
'STIXSizeTwoSym',
'STKaiti',
'STLiti',
'STSong',
'STXihei',
'STXingkai',
'STXinwei',
'STZhongsong',
'Script MT Bold',
'Segoe Fluent Icons',
'Segoe MDL2 Assets',
'Segoe Print',
'Segoe Script',
'Segoe UI',
'Segoe UI Emoji',
'Segoe UI Historic',
'Segoe UI Symbol',
'Segoe UI Variable',
'Showcard Gothic',
'SimHei',
'SimSun',
'SimSun-ExtB',
'Sitka',
'Snap ITC',
'Stencil',
'Sylfaen',
'Symbol',
'Tahoma',
'Tempus Sans ITC',
'Times New Roman',
'Trebuchet MS',
'Tw Cen MT',
'Tw Cen MT Condensed',
'Tw Cen MT Condensed Extra Bold',
'Verdana',
'Viner Hand ITC',
'Vivaldi',
'Vladimir Script',
'Webdings',
'Wide Latin',
'Wingdings',
'Wingdings 2',
'Wingdings 3',
'YouYuan',
'Yu Gothic',
'cmb10',
'cmex10',
'cmmi10',
'cmr10',
'cmss10',
'cmsy10',
'cmtt10',
'icomoon'}
3.基本绘图
x = np.linspace(-5, 5, 50)
x
array([-5. , -4.79591837, -4.59183673, -4.3877551 , -4.18367347,
-3.97959184, -3.7755102 , -3.57142857, -3.36734694, -3.16326531,
-2.95918367, -2.75510204, -2.55102041, -2.34693878, -2.14285714,
-1.93877551, -1.73469388, -1.53061224, -1.32653061, -1.12244898,
-0.91836735, -0.71428571, -0.51020408, -0.30612245, -0.10204082,
0.10204082, 0.30612245, 0.51020408, 0.71428571, 0.91836735,
1.12244898, 1.32653061, 1.53061224, 1.73469388, 1.93877551,
2.14285714, 2.34693878, 2.55102041, 2.75510204, 2.95918367,
3.16326531, 3.36734694, 3.57142857, 3.7755102 , 3.97959184,
4.18367347, 4.3877551 , 4.59183673, 4.79591837, 5. ])
y = x**2
y
array([2.50000000e+01, 2.30008330e+01, 2.10849646e+01, 1.92523948e+01,
1.75031237e+01, 1.58371512e+01, 1.42544773e+01, 1.27551020e+01,
1.13390254e+01, 1.00062474e+01, 8.75676801e+00, 7.59058726e+00,
6.50770512e+00, 5.50812162e+00, 4.59183673e+00, 3.75885048e+00,
3.00916285e+00, 2.34277384e+00, 1.75968347e+00, 1.25989171e+00,
8.43398584e-01, 5.10204082e-01, 2.60308205e-01, 9.37109538e-02,
1.04123282e-02, 1.04123282e-02, 9.37109538e-02, 2.60308205e-01,
5.10204082e-01, 8.43398584e-01, 1.25989171e+00, 1.75968347e+00,
2.34277384e+00, 3.00916285e+00, 3.75885048e+00, 4.59183673e+00,
5.50812162e+00, 6.50770512e+00, 7.59058726e+00, 8.75676801e+00,
1.00062474e+01, 1.13390254e+01, 1.27551020e+01, 1.42544773e+01,
1.58371512e+01, 1.75031237e+01, 1.92523948e+01, 2.10849646e+01,
2.30008330e+01, 2.50000000e+01])
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1d65ba97850>]
4.设置样式和颜色
- 样式:‘-’,‘–’,‘-.’,‘:’,‘.’,‘,’,o,^,v,<,>,s,+,x,D,d,1,2,3,4,h,H,p,|,_
- 颜色:b(蓝色),g(绿色),r(红色),c(青色),m(品红),y(黄色),k(黑色),w(白色)
x = np.linspace(-5, 5, 50)
y = x**2
plt.plot(x,y,c="r")
[<matplotlib.lines.Line2D at 0x1d65ba7d990>]
第二种方式
plt.plot(x,y,color="red")
[<matplotlib.lines.Line2D at 0x1d65c0a7190>]
plt.plot(x,y,c="r",ls="-")
[<matplotlib.lines.Line2D at 0x1d65c7a7850>]
plt.plot(x,y,c="r",ls="--")
[<matplotlib.lines.Line2D at 0x1d65be49f90>]
plt.plot(x,y,c="r",ls="-.")
[<matplotlib.lines.Line2D at 0x1d65be85990>]
plt.plot(x,y,c="r",ls=":")
[<matplotlib.lines.Line2D at 0x1d662ca1e90>]
plt.plot(x,y,"b--")
[<matplotlib.lines.Line2D at 0x1d662ccc150>]
5.画布配置
plt.figure(figsize=(5,3),dpi=100,facecolor="g")
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
plt.grid()
plt.figure(figsize=(5,3))
x = np.linspace(0,8)
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x),"r")
plt.plot(x,-np.sin(x),"g--")
[<matplotlib.lines.Line2D at 0x1d662fd9850>]
plt.figure(figsize=(5,3))
x = np.linspace(0,8)
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x),"r")
plt.plot(x,-np.sin(x),"g--")
plt.show()
plt.figure(figsize=(5,3))
x = np.linspace(0,8)
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x),"r")
plt.show()
plt.plot(x,-np.sin(x),"g--")
[<matplotlib.lines.Line2D at 0x1d663add7d0>]