使用matplotlib绘制简单图表

一、绘制折线图
#导入Matplotlib库中的pyplot模块 
import matplotlib.pyplot as plt

#构造函数
class LineChart:
    def __init__(self, x_data, y_data, title, x_label, y_label):

 #变量
       self.x_data = x_data
        self.y_data = y_data
        self.title = title
        self.x_label = x_label
        self.y_label = y_label

#方法
def plot(self):
        fig, ax = plt.subplots()
        ax.plot(self.x_data, self.y_data)
        ax.set_title(self.title)
        ax.set_xlabel(self.x_label)
        ax.set_ylabel(self.y_label)
        plt.show()



# 示例用法

x_data = [1, 2, 3, 4, 5,6]
y_data = [7, 11, 15, 10, 8,12]

chart = LineChart(x_data, y_data, "折线图示例", "X轴", "Y轴")
chart.plot()
import matplotlib.pyplot as plt

二、绘制柱形图

#导入Matplotlib库中的pyplot模块 
import matplotlib.pyplot as plt

#构造函数
class BarChart:
    def __init__(self, x_data, y_data, title, x_label, y_label):

 #变量
       self.x_data = x_data
        self.y_data = y_data
        self.title = title
        self.x_label = x_label
        self.y_label = y_label

#方法
def plot(self):
        fig, ax = plt.subplots()
        ax.bar(self.x_data, self.y_data)
        ax.set_title(self.title)
        ax.set_xlabel(self.x_label)
        ax.set_ylabel(self.y_label)
        plt.show()



# 示例用法

x_data = ['a', 'b', 'c', 'd', 'e']
y_data = [5, 12, 18, 25,10]

chart = BarChart(x_data, y_data, "柱形图示例", "X轴", "Y轴")
chart.plot()


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值