绘图 matplotlib

plt.plot()绘制线性图

  • 绘制单条线形图
  • 绘制多条线形图
  • 设置坐标系的比例
  • 设置图例legend()
  • 设置轴的标识
  • 图例保存
  • 曲线的样式和风格
1
2
3
4
5
6
import matplotlib.pyplot as plt
import numpy as np
# 绘制单条线形图
x = np.array([1,2,3,4,5])
y = x + 3
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1d441b225f8>]

23Rwd0.jpg

1
2
3
# 绘制多条线形图
plt.plot(x,y)
plt.plot(x+1,y-2)
[<matplotlib.lines.Line2D at 0x1d4419feb38>]

23RdZq.jpg

1
plt.plot(x,y,x+1,y-2)
[<matplotlib.lines.Line2D at 0x1d441abb2b0>,
 <matplotlib.lines.Line2D at 0x1d441abb3c8>]

23RDiT.jpg

1
2
3
# 设置坐标系的比例plt.figure(figsize=(a,b))
plt.figure(figsize=(5,9)) # 放置在绘图的plot方法之前
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1d441beef98>]

23R0oV.jpg

1
2
3
4
# 设置图例legend()
plt.plot(x,y,label='x,y')
plt.plot(x+1,y-2,label='x+1,y-2')
plt.legend() # 图例生效
<matplotlib.legend.Legend at 0x1d441c6f780>

23RNss.jpg

1
2
3
4
5
# 设置轴的标识
plt.plot(x,y)
plt.xlabel('temp')
plt.ylabel('dist')
plt.title('dist&temp')
Text(0.5, 1.0, 'dist&temp')

23RrJU.jpg

1
2
3
4
# 图例保存
fig = plt.figure() # 该对象的创建一定要放置在plot绘图之前
plt.plot(x,y,label='x,y')
fig.savefig('./123.png')

23RsWF.jpg

1
2
# 曲线的样式和风格资料很多,相关参数设置可自行查询按需更改
plt.plot(x,y,c='red',alpha=0.5)
[<matplotlib.lines.Line2D at 0x1d441dc6eb8>]

23Ryz4.jpg

柱状图:plt.bar()

  • 参数:第一个参数是索引。第二个参数是数据值。第三个参数是条形的宽度
1
plt.bar(x,y)
<BarContainer object of 5 artists>

23RcQJ.jpg

1
plt.barh(x,y)
<BarContainer object of 5 artists>

23Rgy9.jpg

直方图

  • 是一个特殊的柱状图,又叫做密度图
  • plt.hist()的参数
    • bins
      可以是一个bin数量的整数值,也可以是表示bin的一个序列。默认值为10
    • normed
      如果值为True,直方图的值将进行归一化处理,形成概率密度,默认值为False
    • color
      指定直方图的颜色。可以是单一颜色值或颜色的序列。如果指定了多个数据集合,例如DataFrame对象,颜色序列将会设置为相同的顺序。如果未指定,将会使用一个默认的线条颜色
    • orientation
      通过设置orientation为horizontal创建水平直方图。默认值为vertical
1
2
data = [1,1,2,2,2,3,4,5,6,6,6,6,6,6,7,8,9,0]
plt.hist(data,bins=20)
(array([1., 0., 2., 0., 3., 0., 1., 0., 1., 0., 0., 1., 0., 6., 0., 1., 0.,
        1., 0., 1.]),
 array([0.  , 0.45, 0.9 , 1.35, 1.8 , 2.25, 2.7 , 3.15, 3.6 , 4.05, 4.5 ,
        4.95, 5.4 , 5.85, 6.3 , 6.75, 7.2 , 7.65, 8.1 , 8.55, 9.  ]),
 <a list of 20 Patch objects>)

23Rfdx.jpg

饼图

  • pie(),饼图也只有一个参数x
  • 饼图适合展示各部分占总体的比例,条形图适合比较各部分的大小
1
2
arr=[11,22,31,15]
plt.pie(arr)
([<matplotlib.patches.Wedge at 0x1d441a7bb00>,
  <matplotlib.patches.Wedge at 0x1d441aefef0>,
  <matplotlib.patches.Wedge at 0x1d441aef8d0>,
  <matplotlib.patches.Wedge at 0x1d441abbdd8>],
 [Text(0.9964244374501308, 0.46598105160209097, ''),
  Text(-0.19579764419425466, 1.0824339622018426, ''),
  Text(-0.830021124093439, -0.7218482759961848, ''),
  Text(0.9100343885615038, -0.617929940717789, '')])

23R2LR.jpg

1
2
arr=[0.2,0.3,0.1]
plt.pie(arr)
([<matplotlib.patches.Wedge at 0x1d441b6fdd8>,
  <matplotlib.patches.Wedge at 0x1d441a72630>,
  <matplotlib.patches.Wedge at 0x1d441c0e5c0>],
 [Text(0.8899186877588753, 0.6465637858537406, ''),
  Text(-0.64656382751384, 0.8899186574910392, ''),
  Text(-1.0461621345079049, -0.3399187966586502, '')])

23Rho6.jpg

1
2
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'])
([<matplotlib.patches.Wedge at 0x1d441d3a4a8>,
  <matplotlib.patches.Wedge at 0x1d441cd02e8>,
  <matplotlib.patches.Wedge at 0x1d441d24748>,
  <matplotlib.patches.Wedge at 0x1d441d24b00>],
 [Text(0.9964244374501308, 0.46598105160209097, 'a'),
  Text(-0.19579764419425466, 1.0824339622018426, 'b'),
  Text(-0.830021124093439, -0.7218482759961848, 'c'),
  Text(0.9100343885615038, -0.617929940717789, 'd')])

23RWe1.jpg

1
2
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3)
([<matplotlib.patches.Wedge at 0x1d441e3ebe0>,
  <matplotlib.patches.Wedge at 0x1d441f0a588>,
  <matplotlib.patches.Wedge at 0x1d441f0aa90>,
  <matplotlib.patches.Wedge at 0x1d441e782e8>],
 [Text(0.2717521193045811, 0.1270857413460248, 'a'),
  Text(-0.05339935750752399, 0.29520926241868434, 'b'),
  Text(-0.2263693974800288, -0.1968677116353231, 'c'),
  Text(0.24819119688041008, -0.16852634746848788, 'd')])

23R5FK.jpg

1
2
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,autopct='%.6f%%')
([<matplotlib.patches.Wedge at 0x1d44201f908>,
  <matplotlib.patches.Wedge at 0x1d44202f0b8>,
  <matplotlib.patches.Wedge at 0x1d44202f7f0>,
  <matplotlib.patches.Wedge at 0x1d44202ff28>],
 [Text(0.2717521193045811, 0.1270857413460248, 'a'),
  Text(-0.05339935750752399, 0.29520926241868434, 'b'),
  Text(-0.2263693974800288, -0.1968677116353231, 'c'),
  Text(0.24819119688041008, -0.16852634746848788, 'd')],
 [Text(0.5435042386091622, 0.2541714826920496, '13.924050%'),
  Text(-0.10679871501504798, 0.5904185248373687, '27.848101%'),
  Text(-0.4527387949600576, -0.3937354232706462, '39.240506%'),
  Text(0.49638239376082016, -0.33705269493697576, '18.987341%')])

23R7Se.jpg

1
2
arr=[11,22,31,15]
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,shadow=True,explode=[0.2,0.3,0.2,0.4])
([<matplotlib.patches.Wedge at 0x1d44207b278>,
  <matplotlib.patches.Wedge at 0x1d44207ba58>,
  <matplotlib.patches.Wedge at 0x1d442089240>,
  <matplotlib.patches.Wedge at 0x1d4420899e8>],
 [Text(0.4529201988409685, 0.21180956891004132, 'a'),
  Text(-0.10679871501504798, 0.5904185248373687, 'b'),
  Text(-0.37728232913338133, -0.32811285272553853, 'c'),
  Text(0.579112792720957, -0.3932281440931384, 'd')])

23RIJO.jpg

散点图scatter()

  • 因变量随自变量而变化的大致趋势
1
2
3
x = np.array([1,3,5,7,9])
y = x ** 2 - 3
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x1d4420c7ba8>

23RoWD.jpg

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2022 Woody
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信