riqilang wu col_name = 'fee' bins = [0, 500, 1500, 3000, 5000, 10000] data[col_name+'_bins'] = pd.cut(data[col_name], bins=bins) df_count = data.groupby(col_name+'_bins')[col_name].count() df_percent = df_count / df_count.sum() * 100 wedges, texts, autotexts = axs[1].pie(df_percent, labels=df_percent.index, autopct='%1.1f%%', startangle=90) axs[1].set_xlabel('Percent of fee')设置以上颜色为浅色,且字体为黑色。给出完整代码
时间: 2024-02-13 08:01:16 浏览: 102
这是一个使用 Pandas 和 Matplotlib 库绘制饼图的示例代码,其中对数据进行了分组和处理,代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
data = pd.read_csv('data.csv')
# 设置分组区间
col_name = 'fee'
bins = [0, 500, 1500, 3000, 5000, 10000]
# 对数据进行分组并统计数量
data[col_name+'_bins'] = pd.cut(data[col_name], bins=bins)
df_count = data.groupby(col_name+'_bins')[col_name].count()
df_percent = df_count / df_count.sum() * 100
# 绘制饼图
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
wedges, texts, autotexts = axs[1].pie(df_percent, labels=df_percent.index, autopct='%1.1f%%', startangle=90)
# 设置颜色和字体
for text in texts:
text.set_color('black')
text.set_fontsize(14)
for autotext in autotexts:
autotext.set_color('black')
autotext.set_fontsize(14)
# 设置标签和标题
axs[1].set_xlabel('Percent of fee', fontsize=16, fontweight='bold')
fig.suptitle('Fee Distribution', fontsize=20, fontweight='bold')
# 显示图像
plt.show()
```
如果要将颜色设置为浅色,可以使用 `wedgeprops` 参数来设置,代码如下:
```python
# 绘制饼图,并设置颜色和字体
wedges, texts, autotexts = axs[1].pie(df_percent, labels=df_percent.index, autopct='%1.1f%%', startangle=90,
wedgeprops={'linewidth': 2, 'edgecolor': 'white', 'alpha': 0.8})
# 设置标签和标题
axs[1].set_xlabel('Percent of fee', fontsize=16, fontweight='bold', color='black')
fig.suptitle('Fee Distribution', fontsize=20, fontweight='bold', color='black')
```
阅读全文