python绘制堆积折线图

本文介绍如何利用Python进行堆积折线图的绘制,适用于数据可视化需求。
摘要由CSDN通过智能技术生成

python绘制堆积折线图

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8]
y = [169, 171, 170, 668, 936, 654, 883, 1806]
y1 = [27, 379, 303
A:Python可以利用pandas和matplotlib等工具绘制簇状柱形图和带标记的堆积折线图绘制簇状柱形图的步骤如下: 1. 读取Excel中的数据到pandas DataFrame中; 2. 根据需要对数据进行处理(例如,按照某一列进行分组等); 3. 利用matplotlib.pyplot模块绘制簇状柱形图; 4. 设置坐标轴标签、图例等,美化图形。 绘制带标记的堆积折线图的步骤类似,只需要利用matplotlib.pyplot模块的plot函数绘制折线,使用stackplot函数绘制堆积图,然后设置标记等即可。 下面是一个例子: ``` import pandas as pd import matplotlib.pyplot as plt # 读取Excel数据 data = pd.read_excel('sample.xlsx') # 按照'Region'列进行分组计算各项指标的总和 grouped_data = data.groupby('Region').sum() # 绘制簇状柱形图 width = 0.35 # 柱形宽度 ind = range(len(grouped_data)) # 横坐标刻度 fig, ax = plt.subplots() rect1 = ax.bar(ind, grouped_data['Sales'], width, color='r', label='Sales') rect2 = ax.bar([i + width for i in ind], grouped_data['Profit'], width, color='b', label='Profit') # 设置坐标轴标签、图例等 ax.set_xticks([i + width / 2 for i in ind]) ax.set_xticklabels(grouped_data.index) ax.set_xlabel('Region') ax.set_ylabel('Amount') ax.legend() # 绘制带标记的堆积折线图 fig, ax = plt.subplots() ax.plot(grouped_data.index, grouped_data['Sales'], marker='o', label='Sales') ax.stackplot(grouped_data.index, grouped_data['Profit'], grouped_data['Shipping Cost'], labels=['Profit', 'Shipping Cost']) ax.set_xlabel('Region') ax.set_ylabel('Amount') ax.legend() plt.show() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值