Matplotlib初体验:用线条和颜色给数据穿上‘笑’果十足的霓裳,让图表不再沉闷!

在这里插入图片描述

1.导包

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

2.运行时配置参数

  • rcParams : runtime configuration Parameters
  • 运行时配置参数
# 运行时配置参数
# rcParams : runtime configuration Parameters

# 如果浏览器不显示图片,就需要加上这句话
%matplotlib inline

# 让图片中可以显示中文
plt.rcParams['font.sans-serif'] = "SimHei"
# 让图片中可以显示负号
plt.rcParams["axes.unicode_minus"] = False

# 支持svg矢量图
%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.基本绘图

  • plt.plot()
# 抛物线
x = np.linspace(-5, 5, 50)   # 等差数列:-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>]

在这里插入图片描述

# ls:line style 设置样式红色实线
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:画布大小,宽高比例
# dpi:分辨率,像素密度
# facecolor:背景颜色
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--")
# show:放在最后会一起画
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")
# show:放在中间,会分开画,前面的先画,后面的使用新的画布再画
plt.show()

plt.plot(x,-np.sin(x),"g--")

在这里插入图片描述

[<matplotlib.lines.Line2D at 0x1d663add7d0>]

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾飞开源

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值