用matplotlib面向对象绘制图表

1、虚线平均温度 

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4,19)
y_max = np.array([32,33,34,34,33,31,30,29,30,29,26,23,21,25,31])
y_min = np.array([19,19,20,22,22,21,22,16,18,18,17,14,15,16,16])
y_med = (y_max + y_min) /2

plt.plot(x,y_med,'r--')
plt.plot(x,y_max)
plt.plot(x,y_min)

plt.show()

 2、柱形图

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
yl = np.array([10,8,7,11,13])
y2 = np.array([9,6,5,10,12])

bar_width = 0.3

plt.bar(x,yl, tick_label=['a','b','c','d','e'], width=bar_width)
plt.bar(x+bar_width, y2, width=bar_width)


plt.show()

 3、带误差棒的堆积柱形图

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
yl = np.array([10,8,7,11,13])
y2 = np.array([9,6,5,10,12])

bar_width = 0.3
erroe = [2,1,2.5,2,1.5]

plt.bar(x,yl, tick_label=['a','b','c','d','e'], width=bar_width)
plt.bar(x, y2, bottom=yl, width=bar_width, yerr=erroe)


plt.show()

4、多组条形图

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(5)
xl = np.array([10,8,7,11,13])
x2 = np.array([9,6,5,10,12])

bar_height = 0.3

plt.barh(y,xl, tick_label=['a','b','c','d','e'], height=bar_height)
plt.barh(y+bar_height, x2, height=bar_height)


plt.show()

 

 5、带误差棒的条形图

import matplotlib.pyplot as plt
import numpy as np

y = np.arange(5)
xl = np.array([10,8,7,11,13])
x2 = np.array([9,6,5,10,12])

bar_height = 0.3
erroe = [2,1,2.5,2,1.5]

plt.barh(y,xl, tick_label=['a','b','c','d','e'], height=bar_height)
plt.barh(y, x2, left=xl, height=bar_height, xerr=erroe)


plt.show()

6、堆积面积图

import matplotlib.pyplot as plt
import numpy as np


x = np.arange(6)
y1 = np.array([1,4,3,5,6,7])
y2 = np.array([1,3,4,2,7,6])
y3 = np.array([3,4,3,6,5,5])


plt.stackplot(x,y1,y2,y3)

plt.show()

7、中文代码

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,关于你提到的问题,我来简单说明一下。 Matplotlib 是一个 Python 绘图库,支持多种类型的图表绘制和自定义化配置。其中,Matplotlib 提供了两种绘图方式,分别是面向对象和面向过程。 面向过程方式是基于一系列的函数调用,帮助我们绘制出我们想要的图表。例如,我们可以通过函数 `plt.plot()` 绘制一条线条,通过函数 `plt.xlabel()` 设置 X 轴标签,如下所示: ```python import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制线条 plt.plot(x, y) # 设置 X 轴标签 plt.xlabel('X-axis') # 展示图表 plt.show() ``` 而面向对象方式则是通过创建图表对象并对其进行操作,使得我们可以更加灵活、直观地绘制出复杂图表。例如,我们可以通过代码如下方式绘制出一张含有两个具有不同样式和颜色的线条的图表: ```python import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [-1, -2, -3, -4, -5] # 创建图表对象 fig, ax = plt.subplots() # 绘制线条 ax.plot(x, y1, color='red', linestyle='-', label='Line 1') ax.plot(x, y2, color='blue', linestyle='--', label='Line 2') # 设置 X 轴标签和图例 ax.set_xlabel('X-axis') ax.legend() # 展示图表 plt.show() ``` 总的来说,两种方式各有优劣,面向过程方式简单易用,适用于快速绘制简单的图表,而面向对象方式则更适合于复杂场景下的绘图和定制化配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值