python绘制并行折线图
时间: 2023-09-24 20:03:43 浏览: 91
要使用Python绘制并行折线图,你可以使用matplotlib库。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [2, 4, 6, 8, 10]
y3 = [3, 6,9, 12, 15]
# 创建图形和坐标轴对象
fig, ax = plt.subplots()
# 绘制折线图
ax.plot(x, y1, label='Line 1')
ax.plot(x, y2, label='Line 2')
ax.plot(x, y3, label='Line 3')
# 添加标题和标签
ax.set_title('Parallel Line Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# 添加图例
ax.legend()
# 显示图形
plt.show()
```
在这个示例中,我们创建了一个包含三条线的并行折线图。每条线都有相应的x和y坐标。通过调用`plot`函数和传递对应的x和y值来绘制每条线。然后,我们添加了标题、坐标轴标签和图例。最后,调用`show`函数显示图形。
你可以根据自己的数据和需求进行修改和调整。希望对你有所帮助!
相关问题
使用pyecharts,由csv文件a.csv,完成下列问题(1)、获取处理好的数据 (2)、设置系列配置项和全局配置项,绘制销量前5的商品数量柱形图 (3)、设置系列配置项和全局配置项,绘制售货机每月总交易额折线图 (4)、设置系列配置项和全局配置项,绘制售货机各类(按照小类)商品的销售额饼图 (5)、设置系列配置项和全局配置项,绘制2017年每月每台售货机的销售额的时间线轮播多图 (6)、设置系列配置项和全局配置项,绘制售货机每月各类(按照大类)商品的销售额柱形图与饼图的并行多图。
由于题目中提到的数据文件a.csv不清楚具体内容和格式,因此以下示例仅提供代码框架供参考,实际应用需要根据具体数据格式进行相应的数据预处理和可视化操作。
(1)获取处理好的数据
```python
import pandas as pd
# 读取CSV文件
df = pd.read_csv('a.csv')
# 对数据进行处理,获取销量前5的商品数量
top5_products = df.groupby('商品名称')['销量'].sum().sort_values(ascending=False)[:5]
```
(2)绘制销量前5的商品数量柱形图
```python
from pyecharts import options as opts
from pyecharts.charts import Bar
# 创建柱形图
bar_chart = Bar()
# 添加x轴数据和y轴数据
bar_chart.add_xaxis(top5_products.index.tolist())
bar_chart.add_yaxis('销量', top5_products.tolist())
# 设置全局配置项
bar_chart.set_global_opts(
title_opts=opts.TitleOpts(title='销量前5的商品数量柱形图'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15))
)
# 设置系列配置项
bar_chart.set_series_opts(
label_opts=opts.LabelOpts(position='top')
)
# 显示图表
bar_chart.render()
```
(3)绘制售货机每月总交易额折线图
```python
from pyecharts.charts import Line
# 对数据进行处理,获取每月总交易额
monthly_sales = df.groupby('交易时间月份')['交易额'].sum()
# 创建折线图
line_chart = Line()
# 添加x轴数据和y轴数据
line_chart.add_xaxis(monthly_sales.index.tolist())
line_chart.add_yaxis('交易额', monthly_sales.tolist())
# 设置全局配置项
line_chart.set_global_opts(
title_opts=opts.TitleOpts(title='售货机每月总交易额折线图')
)
# 设置系列配置项
line_chart.set_series_opts(
label_opts=opts.LabelOpts(is_show=False)
)
# 显示图表
line_chart.render()
```
(4)绘制售货机各类商品的销售额饼图
```python
from pyecharts.charts import Pie
# 对数据进行处理,获取各类商品的销售额
category_sales = df.groupby('商品小类')['销售额'].sum()
# 创建饼图
pie_chart = Pie()
# 添加数据
pie_chart.add(
'',
[list(z) for z in zip(category_sales.index.tolist(), category_sales.tolist())],
radius=['30%', '75%'],
rosetype='radius'
)
# 设置全局配置项
pie_chart.set_global_opts(
title_opts=opts.TitleOpts(title='售货机各类商品的销售额饼图'),
legend_opts=opts.LegendOpts(
orient='vertical',
pos_top='15%',
pos_left='2%'
)
)
# 设置系列配置项
pie_chart.set_series_opts(
label_opts=opts.LabelOpts(formatter='{b}: {c} ({d}%)')
)
# 显示图表
pie_chart.render()
```
(5)绘制2017年每月每台售货机的销售额的时间线轮播多图
```python
from pyecharts.charts import Timeline
# 对数据进行处理,获取2017年每月每台售货机的销售额
monthly_sales_by_vending_machine = df[df['交易时间年份'] == 2017].groupby(['交易时间月份', '售货机编号'])['销售额'].sum().unstack()
# 创建时间线轮播多图
timeline_chart = Timeline()
# 添加每个时间点的图表
for month in monthly_sales_by_vending_machine.index.tolist():
bar_chart = (
Bar()
.add_xaxis(monthly_sales_by_vending_machine.columns.tolist())
.add_yaxis('', monthly_sales_by_vending_machine.loc[month].tolist())
.set_global_opts(
title_opts=opts.TitleOpts(title='{}年{}月每台售货机的销售额'.format(2017, month)),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15))
)
.set_series_opts(
label_opts=opts.LabelOpts(position='top')
)
)
timeline_chart.add(bar_chart, '{}年{}月'.format(2017, month))
# 显示图表
timeline_chart.render()
```
(6)绘制售货机每月各类商品的销售额柱形图与饼图的并行多图
```python
from pyecharts.charts import Grid
# 对数据进行处理,获取每月各类商品的销售额
monthly_sales_by_category = df.groupby(['交易时间月份', '商品大类', '商品小类'])['销售额'].sum().reset_index()
# 创建并行多图
grid_chart = (
Grid()
.add(
# 创建柱形图
Bar()
.add_xaxis(monthly_sales_by_category[monthly_sales_by_category['商品大类'] == '饮料']['商品小类'].unique().tolist())
.add_yaxis('饮料', monthly_sales_by_category[monthly_sales_by_category['商品大类'] == '饮料']['销售额'].tolist())
.add_yaxis('零食', monthly_sales_by_category[monthly_sales_by_category['商品大类'] == '零食']['销售额'].tolist())
.set_global_opts(
title_opts=opts.TitleOpts(title='售货机每月各类商品的销售额柱形图与饼图的并行多图(柱形图部分)'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=15))
)
.set_series_opts(
label_opts=opts.LabelOpts(position='top')
),
grid_opts=opts.GridOpts(pos_left='5%', pos_right='55%', pos_top='15%')
)
.add(
# 创建饼图
Pie()
.add(
'',
[list(z) for z in zip(
monthly_sales_by_category[monthly_sales_by_category['商品大类'] == '饮料']['商品小类'].tolist(),
monthly_sales_by_category[monthly_sales_by_category['商品大类'] == '饮料']['销售额'].tolist()
)],
center=['75%', '50%'],
radius=['30%', '75%'],
rosetype='radius'
)
.set_global_opts(
title_opts=opts.TitleOpts(title='售货机每月各类商品的销售额柱形图与饼图的并行多图(饼图部分)'),
legend_opts=opts.LegendOpts(
orient='vertical',
pos_top='15%',
pos_left='80%'
)
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter='{b}: {c} ({d}%)')
),
grid_opts=opts.GridOpts(pos_left='60%', pos_right='5%', pos_top='15%')
)
)
# 显示图表
grid_chart.render()
```
阅读全文
相关推荐

















