删除条形图中条形之间的空格
Remove spaces between bars in barchart
我希望图表上的条形之间没有空格。我在发布之前搜索了这个问题,虽然有各种各样的答案,但 none 对我的图表有影响。我假设我做错了什么,但我不知道如何让它发挥作用。
这两种方法我都试过了,横条之间的空格没有改变:
plt.axis('tight')
bin = np.arange(my_list)
plt.xlim([0, bin.size])
这是我的代码:
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots()
width = .35
Kitchen = ("Cucumber", "Legacy", "Monkey")
y_pos = np.arange(len(Kitchen))
barlist = ax.bar(y_pos, my_list, width, align='center', ecolor='black')
barlist[0].set_color('dodgerblue')
barlist[1].set_color('orangered')
barlist[2].set_color('khaki')
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(barlist)
plt.title('My Bar Chart')
plt.xlabel("Kitchen")
plt.ylabel("Shoes")
plt.xticks(y_pos,("Cucumber", "Legacy", "Monkey",))
plt.show()
恰当命名的变量width
是您需要修改的。如果您的列表有点长,您还可以提供颜色作为未来的参考。
import matplotlib.pyplot as plt
import numpy as np
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots()
N = len(my_list)
ind = np.arange(N)
width = 0.99
## the bars
colors = ['dodgerblue','orangered','khaki']
barlist = ax.bar(ind, my_list, width, color=colors)
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(barlist)
# axes and labels
xtick_marks = ["Cucumber", "Legacy", "Monkey"]
xtick_names = ax.set_xticklabels(xtick_marks)
ax.set_xticks(ind)
xbuffer = 3
ax.set_xlim(-xbuffer,len(ind)-1+xbuffer)
plt.show()
将 width
设置为 1.0 完全没有 space,但在 0.99 时可能在视觉上更具吸引力。
将 xlim 设置为使用缓冲区缩放将允许您根据自己的喜好放大或缩小。
我想出了一个解决这个问题的办法。我添加了粗体部分,这最终让我可以调整图表的大小以去除多余的白色 space。
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots(**figsize=(3, 3.5)**)
width = .35
我希望图表上的条形之间没有空格。我在发布之前搜索了这个问题,虽然有各种各样的答案,但 none 对我的图表有影响。我假设我做错了什么,但我不知道如何让它发挥作用。
这两种方法我都试过了,横条之间的空格没有改变:
plt.axis('tight')
bin = np.arange(my_list)
plt.xlim([0, bin.size])
这是我的代码:
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots()
width = .35
Kitchen = ("Cucumber", "Legacy", "Monkey")
y_pos = np.arange(len(Kitchen))
barlist = ax.bar(y_pos, my_list, width, align='center', ecolor='black')
barlist[0].set_color('dodgerblue')
barlist[1].set_color('orangered')
barlist[2].set_color('khaki')
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(barlist)
plt.title('My Bar Chart')
plt.xlabel("Kitchen")
plt.ylabel("Shoes")
plt.xticks(y_pos,("Cucumber", "Legacy", "Monkey",))
plt.show()
恰当命名的变量width
是您需要修改的。如果您的列表有点长,您还可以提供颜色作为未来的参考。
import matplotlib.pyplot as plt
import numpy as np
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots()
N = len(my_list)
ind = np.arange(N)
width = 0.99
## the bars
colors = ['dodgerblue','orangered','khaki']
barlist = ax.bar(ind, my_list, width, color=colors)
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(barlist)
# axes and labels
xtick_marks = ["Cucumber", "Legacy", "Monkey"]
xtick_names = ax.set_xticklabels(xtick_marks)
ax.set_xticks(ind)
xbuffer = 3
ax.set_xlim(-xbuffer,len(ind)-1+xbuffer)
plt.show()
将 width
设置为 1.0 完全没有 space,但在 0.99 时可能在视觉上更具吸引力。
将 xlim 设置为使用缓冲区缩放将允许您根据自己的喜好放大或缩小。
我想出了一个解决这个问题的办法。我添加了粗体部分,这最终让我可以调整图表的大小以去除多余的白色 space。
my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots(**figsize=(3, 3.5)**)
width = .35