用面向对象绘制折线图和柱形图

import matplotlib.pyplot as plt

# 示例数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 创建一个新的Figure和Axes对象
fig, ax = plt.subplots()

# 绘制折线图
ax.plot(x, y, label='折线图', marker='o', linestyle='-', color='b')

# 设置图表标题和坐标轴标签
ax.set_title('折线图示例')
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')

# 添加图例
ax.legend()

# 显示图表
plt.show()

# 创建一个新的Figure和Axes对象
fig2, ax2 = plt.subplots()

# 示例散点图数据
x2 = [1, 2, 3, 4, 5]
y2 = [3, 6, 1, 8, 5]

# 绘制散点图
ax2.scatter(x2, y2, label='散点图', color='r', marker='x')

# 设置图表标题和坐标轴标签
ax2.set_title('散点图示例')
ax2.set_xlabel('X轴')
ax2.set_ylabel('Y轴')

# 添加图例
ax2.legend()

# 显示散点图
plt.show()

#导入matplotlib.pyplot并重新命名
import matplotlib.pyplot as plt
#导入numpy并重新命名
import numpy as np
#设置X
x=np.arange(7)
#设置Y1
y1=np.array([14,15,16,19,24,22,10])
#设置Y2
y2=np.array([19,17,16,26,9,24,31])
#设置柱形图底部间隔
bar_width=0.3
#设置底部标签
labels=['a','b','c','d','e','f','g']
#设置画布
fig=plt.figure()
#面向对象接口
ax=fig.add_subplot(111)
#设置Y1柱子的属性
ax.bar(x,y1,tick_label=labels,width=bar_width,color='blue')
#设置Y2柱子的属性
ax.bar(x+bar_width,y2,tick_label=labels,width=bar_width,color='red')
#绘制图像
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值