import pandas as pd from pyecharts.charts import Pie from pyecharts import options as opts # 读取数据 data = pd.read_csv('东京购物清除暂无评分后的表格.csv') # 统计第二列数据的出现次数 count1 = data['score'].value_counts() # 生成第一个饼图 pie1 = Pie() pie1.add("个", [list(z) for z in zip(count1.index.tolist(), count1.values.tolist())]) pie1.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) pie1.set_global_opts(title_opts=opts.TitleOpts(title="购物评分数量统计饼图")) # 保存成网页形式 pie1.render("日本购物饼图.html")把这个饼图变为环形图,且加上百分比
时间: 2023-06-19 16:08:56 浏览: 328
可以通过添加 `radius` 参数来将饼图转换为环形图,并添加 `formatter` 参数来显示百分比。
修改代码如下:
```python
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
# 读取数据
data = pd.read_csv('东京购物清除暂无评分后的表格.csv')
# 统计第二列数据的出现次数
count1 = data['score'].value_counts()
# 生成第一个环形图
pie1 = Pie()
pie1.add(
"个",
[list(z) for z in zip(count1.index.tolist(), count1.values.tolist())],
radius=["40%", "55%"] # 设置内外半径
)
pie1.set_series_opts(
label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)") # 显示百分比
)
pie1.set_global_opts(
title_opts=opts.TitleOpts(title="购物评分数量统计环形图")
)
# 保存成网页形式
pie1.render("日本购物环形图.html")
```
运行后生成的图表将是一个环形图,且每个扇形显示的数据为绝对数和百分比。
相关问题
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
好的,以下是将代码转换为使用 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。
import pandas as pd from pyecharts import options as opts from pyecharts.charts import Pie df = pd.read_csv("D:\pythonProject2\eijing_AQI_2018.csv") rank_message = df.groupby(['Quality_grade']) rank_com = rank_message['Quality_grade'].agg(['count']) rank_com.reset_index(inplace=True) rank_com_last = rank_com.sort_values('count', ascending=False) attr = rank_com_last['Quality_grade'] v1 = rank_com_last['count'] pie = ( Pie() .add("空气质量", [list(z) for z in zip(attr, v1)], radius=[80, 180], tooltip_opts=opts.TooltipOpts(textstyle_opts=opts.TextStyleOpts(align='center'), formatter='{a}' + '<br/>' + '{b}: {c} ({d}%)')) .set_global_opts(title_opts=opts.TitleOpts(title='2018年北京全年空气质量情况', pos_left='center'), legend_opts=opts.LegendOpts(orient='vertical', pos_top='5%', pos_left='2%'), ) ) pie.render("北京全年空气质量等级占比饼图.html")
这段代码是使用 Python 中的 pandas 和 pyecharts 库绘制北京市 2018 年空气质量情况的饼图。首先使用 pandas 读取 csv 文件,然后按照空气质量等级进行分组统计,得到每个等级对应的数据个数。然后使用 pyecharts 中的 Pie 类创建饼图,设置数据源和参数,最后调用 render 方法生成 html 文件。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)