Python 3.x 堆叠条形图-y轴和第一条条形图之间的空间:matplotlib.pyplot
我刚开始使用matplotlib.pyplot,有点卡住了 使用matpltlib.pyplot文档中的示例,我使用以下代码创建了一个堆叠条形图:Python 3.x 堆叠条形图-y轴和第一条条形图之间的空间:matplotlib.pyplot,python-3.x,matplotlib,Python 3.x,Matplotlib,我刚开始使用matplotlib.pyplot,有点卡住了 使用matpltlib.pyplot文档中的示例,我使用以下代码创建了一个堆叠条形图: import numpy as np import matplotlib.pyplot as plt N = 7 OECD = (242, 244, 255, 263, 269, 276, 285) NonOECD = (282, 328, 375, 417, 460, 501, 535) Sum = ('524', '572', '630', '
import numpy as np
import matplotlib.pyplot as plt
N = 7
OECD = (242, 244, 255, 263, 269, 276, 285)
NonOECD = (282, 328, 375, 417, 460, 501, 535)
Sum = ('524', '572', '630', '680', '729', '777', '820')
ind = np.arange(N)
width = 0.5
p1 = plt.bar(ind, NonOECD, width, color = 'r')
p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD)
plt.ylabel('Quadrillion Btu')
plt.title('World Total Energy Consumption 2010 - 2040')
plt.xticks(ind+width/2., ('2010', '2015', '2020', '2025', '2030', '2035', '2040'))
plt.yticks(np.arange(0, 1001, 200))
plt.legend((p1[0], p2[0]), ('Non - OECD', 'OECD'), loc = 2, frameon = 'false')
plt.tick_params(top = 'off', bottom = 'off', right = 'off')
plt.grid(axis = 'y', linestyle = '-')
plt.show()
但是,如果第一根横杆(2010年)不是正好对着y轴,我会更喜欢它。我尝试在plt1和plt2中简单地向ind添加1
i、 e 但是,我无法计算出标签的等效变化。到目前为止,我所做的只是: 话虽如此,我可以通过使N=8,在两个元组中增加一个额外的零第一项来回避这个问题?OECD和非OECD,并添加一个空白xticklabel:
i、 e 但是,我无法使用此软糖,因为我想显示总数。…您想使用“边距”功能。您的代码已修改:
fig = plt.figure()
ax = fig.add_subplot(111)
# the first argument is the margin of the x-axis, the second of the y-axis
ax.margins(0.04, 0)
p1 = ax.bar(ind, NonOECD, width, color = 'r')
p2 = ax.bar(ind, OECD, width, color = 'b', bottom = NonOECD)
您想使用“边距”功能。您的代码已修改:
fig = plt.figure()
ax = fig.add_subplot(111)
# the first argument is the margin of the x-axis, the second of the y-axis
ax.margins(0.04, 0)
p1 = ax.bar(ind, NonOECD, width, color = 'r')
p2 = ax.bar(ind, OECD, width, color = 'b', bottom = NonOECD)
您想要使用
plt.xlim
或ax.set\u xlim
您想要使用plt.xlim
或ax.set\u xlim
fig = plt.figure()
ax = fig.add_subplot(111)
# the first argument is the margin of the x-axis, the second of the y-axis
ax.margins(0.04, 0)
p1 = ax.bar(ind, NonOECD, width, color = 'r')
p2 = ax.bar(ind, OECD, width, color = 'b', bottom = NonOECD)