from pyecharts.charts import Line from pyecharts.charts import Bar from pyecharts.charts import Pie from pyecharts.charts import Grid from pyecharts import options as opts # 柱状图 from pyecharts.charts import Bar from pyecharts import options as opts bar=Bar() bar.add_xaxis(["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"]) bar.add_yaxis("商家A",[120,56,28,98,129,28,107]) bar.add_yaxis("商家B",[60,140,153,145,160,70,54]) bar.set_global_opts(title_opts=opts.TitleOpts(title="商家A和商家B销售情况柱状图")) bar.render() # 折线图 from pyecharts.charts import Line from pyecharts import options as opts from pyecharts.globals import ThemeType,RenderType x_data=['1月','2月','3月','4月','5月','6月'] y_data1=[2,4.9,7,23.2,25.6,76.7] y_data2=[2.6,5.9,9,26.4,28.7,70.7] y_data3=[2,2.2,3.3,4.5,6.3,10.2] line=( Line() .add_xaxis(x_data) .add_yaxis('蒸发量',y_data1) .add_yaxis('降水量',y_data2) .add_yaxis('平均温度',y_data3) ) line.render("zhexiantu.html") # 饼图 from pyecharts.faker import Faker from pyecharts.charts import Pie import pyecharts.options as opts Faker.choose() Faker.values() list(zip(Faker.choose(),Faker.values())) pie=( Pie() .add('',[list(x) for x in zip(["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],[120,56,28,98,129,28,107])],center=[450,300],radius=[0,100]) ) pie.set_global_opts(title_opts=opts.TitleOpts(title='销售情况饼图',subtitle='商家A')) pie.render() # 多图布局 grid=( Grid() .add(bar,grid_opts=opts.GridOpts(pos_left="70%")) .add(line,grid_opts=opts.GridOpts(pos_right="70%")) .add(pie,grid_opts=opts.GridOpts(pos_bottom="80%")) ) grid.render("2102010216-刘云龙.html")换成matplotlib和pandas
时间: 2024-03-07 15:47:41 浏览: 94
好的,以下是将代码转换为使用 matplotlib 和 pandas 的版本:
```python
import matplotlib.pyplot as plt
import pandas as pd
# 柱状图
df_bar = pd.DataFrame({
'商品': ["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],
'商家A': [120,56,28,98,129,28,107],
'商家B': [60,140,153,145,160,70,54]
})
ax = df_bar.plot(x='商品', kind='bar')
ax.set_title('商家A和商家B销售情况柱状图')
plt.show()
# 折线图
df_line = pd.DataFrame({
'月份': ['1月','2月','3月','4月','5月','6月'],
'蒸发量': [2,4.9,7,23.2,25.6,76.7],
'降水量': [2.6,5.9,9,26.4,28.7,70.7],
'平均温度': [2,2.2,3.3,4.5,6.3,10.2]
})
ax = df_line.plot(x='月份', kind='line')
ax.set_title('蒸发量、降水量和平均温度折线图')
plt.show()
# 饼图
df_pie = pd.DataFrame({
'商品': ["衬衫","毛衣","领带","裤子","风衣","高跟鞋","袜子"],
'销售量': [120,56,28,98,129,28,107]
})
ax = df_pie.plot(kind='pie', y='销售量', labels=df_pie['商品'], autopct='%1.1f%%', startangle=90)
ax.set_title('销售情况饼图')
plt.show()
# 多图布局
fig, axs = plt.subplots(ncols=3)
# 柱状图
df_bar.plot(x='商品', kind='bar', ax=axs[0])
axs[0].set_title('商家A和商家B销售情况柱状图')
# 折线图
df_line.plot(x='月份', kind='line', ax=axs[1])
axs[1].set_title('蒸发量、降水量和平均温度折线图')
# 饼图
df_pie.plot(kind='pie', y='销售量', labels=df_pie['商品'], autopct='%1.1f%%', startangle=90, ax=axs[2])
axs[2].set_title('销售情况饼图')
plt.tight_layout()
plt.show()
```
注意:由于 pandas 的 `plot()` 函数默认使用的是 matplotlib,因此在使用 pandas 绘图时其实已经间接使用了 matplotlib。
阅读全文