与此处解释和显示的示例相比,我试图获得非常相似的输出:https ://matplotlib.org/gallery/pie_and_polar_charts/bar_of_pie.html?highlight=bar%20pie
到目前为止,一切都很好。我什至设法获得了第二个条形图,但显然用于格式化第二个图的命令被忽略了。下面显示的代码给出了以下输出:
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
import math
fig = plt.figure(figsize=(24,8))
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
#pie chart
labels = ['Completed', 'Not completed']
ratios = [27.09, 72.91]
startangle = -45
explode = [0.1, 0] # only "explode" the 2nd slice (i.e. 'Hogs')
ax1.pie(ratios, autopct='%1.1f%%', explode=explode, labels=labels, startangle=startangle)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
#bar chart #1
xpos = 0
bottom = 0
ratio_boys = [45.0, 55.0]
width = 0.1
colors = ['red', 'grey']
for j in range(len(ratio_boys)):
height = ratio_boys[j]
ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
ypos = bottom + ax2.patches[j].get_height()/2
bottom += height
ax2.text(xpos,ypos, "%1.1f%%" %
(ax2.patches[j].get_height()), ha='center')
plt.title('Boys')
ax2.axis('off')
plt.xlim(-2.5*width, 2.5*width)
#bar chart #2
xpos = 0
bottom = 0
ratio_girls = [8.6, 91.4]
width = 0.1
colors = ['red', 'grey']
for j in range(len(ratio_girls)):
height = ratio_girls[j]
ax3.bar(xpos, height, width, bottom=bottom, color=colors[j])
ypos = bottom + ax3.patches[j].get_height()/2
bottom += height
ax3.text(xpos,ypos, "%1.1f%%" %
(ax3.patches[j].get_height()), ha='center')
plt.title('Girls', fontsize=14, fontweight='bold')
ax3.axis('off')
plt.xlim(-2.5*width, 2.5*width)
所需的输出应该看起来略有不同。像这样:
我必须改变什么?左上边框的这个 (-0.25, 0.25) 是什么意思?这是从哪里来的?

