python - 在 MatPlotLib 中并排绘制 2 个饼图

标签 python python-3.x numpy matplotlib

我目前正尝试在 Matplotlib 中创建 2 个饼图。虽然目前我的代码允许我打印出 2 个饼图,但它们是在垂直方向上打印的。有什么方法可以使饼图水平并排打印?

这是我目前的代码,只允许我垂直打印 2 个饼图:

import matplotlib.pyplot as plt
import numpy as np

#read the csv file
filename = '../LAB03-DATA VISUALIZATION USING MATPLOTLIB/singapore-residents-by-ethnic-group-and-sex-end-june-annual.csv'
data = np.genfromtxt(filename, dtype=['i8','U50','i8'], delimiter=',', names=True)

#extract datas that are of 1960
data_1960 = data[data['year'] == 1960]

#extract datas that are of 2016
data_2016 = data[data['year'] == 2016]

#extract datas that have this keyword 'TOTAL MALE RESIDENTS' and 'TOTAL FEMALE RESIDENTS'(1960)
male_and_female_1960 = data_1960[np.isin(data_1960['level_1'], ['Total Male Residents' , 'Total Female Residents'])]

#extract datas that have this keyword 'TOTAL MALE RESIDENTS' and 'TOTAL FEMALE RESIDENTS'(2016)
male_and_female_2016 = data_2016[np.isin(data_2016['level_1'], ['Total Male Residents' , 'Total Female Residents'])]

# PLOTTING OF THE 1960 PIE CHART-------------------------------------------------------------------------------------------------
labels = male_and_female_1960['level_1']
values = male_and_female_1960['value']

#settings and configs for the pie charts
colors = ['#FF8F33','#33FFDC']
explode = (0.1, 0)

plt.figure(figsize=(5,5))
plt.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%')
plt.title('Gender Composition in 1960')

# PLOTTING OF THE 2016 PIE CHART-------------------------------------------------------------------------------------------------
labels = male_and_female_2016['level_1']
values = male_and_female_2016['value']

#settings and configs for the pie charts
colors = ['#FF8F33','#33FFDC']
explode = (0.1, 0)

plt.figure(figsize=(5,5))
plt.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%')
plt.title('Gender Composition in 2016')

plt.show() 

我当前的输出。

Pie Chart Image Output

最佳答案

是的,你可以 -

fig, (ax1,ax2) = plt.subplots(1,2,figsize=(10,10)) #ax1,ax2 refer to your two pies

# 1,2 denotes 1 row, 2 columns - if you want to stack vertically, it would be 2,1

labels = male_and_female_1960['level_1']
values = male_and_female_1960['value']
ax1.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%') #plot first pie
ax1.title('Gender Composition in 1960')


labels = male_and_female_2016['level_1']
values = male_and_female_2016['value']
ax2.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%') #plot second pie
ax2.title('Gender Composition in 2016')

关于python - 在 MatPlotLib 中并排绘制 2 个饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887571/

相关文章:

python - 重新启动以循环迭代列表python3

Python 和 shell 命令

python - 从 Unicode 字符串中正确提取表情符号

python - 如何在 Python 中进行动态类生成? (或者一系列 if/else 会更好)

python-3.x - python3 ftplib SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:645)')

python-3.x - OpenCV xfeatures2d_SURF -213 :The function/feature is not implemented

python - 如何将不规则间隔的 x 和 y 坐标张量平均到具有特定单元大小的网格中?

python - 用零替换 numpy 数组中低于阈值 # 的数字

python - 使用 Numpy 更有效地改变色调

numpy recarray 副本保留 dtype 引用?