seaborn 中的方形条形图

时间:2021-07-06 07:26:47

标签: python matplotlib plot seaborn

我正在尝试在 seaborn 中创建一个正方形和等比例的条形图,但到目前为止没有成功。我将不胜感激任何帮助。下面是代码示例:

time = [12, 21, 12, 31, 5, 4, 26]
run = ['A', 'B', 'c', 'D', 'E', 'F', 'G']
ax = sns.barplot(run, time)
ax.set_ylabel("Seconds", fontsize=10)
plt.xticks(rotation=90)
ax.set(yticks=[0, 10, 20, 30, 40])
ax.axis('square')
plt.tight_layout()
plt.show()

enter image description here

非常感谢!

2 个答案:

答案 0 :(得分:2)

直接删除

ax.axis('square')

这应该很好。

fig = plt.figure(figsize=(10, 8))
time = [12, 21, 12, 31, 5, 4, 26]
run = ['A', 'B', 'c', 'D', 'E', 'F', 'G']
ax = sns.barplot(x =run, y= time)
ax.set_ylabel("Seconds", fontsize=10)
plt.xticks(rotation=90)
ax.set(yticks=[0, 10, 20, 30, 40])
plt.tight_layout()
plt.show()

答案 1 :(得分:1)

问题在于 seaborn 的 barplot 自动将 x 变量视为分类变量,因此将它们绘制在 0、1、2、...

我不知道有什么方法可以在 seaborn 中禁用这种行为,所以我建议只使用 matplotlib 的 bar,它允许您明确指定 x 位置。使用 xnp.linspace 位置隔开,并通过 tick_label=run:

更改标签
x = np.linspace(0, max(time), len(time))
color = sns.color_palette('Dark2')

fig, ax = plt.subplots()
ax.bar(x, time, tick_label=run, color=color, width=4)
ax.axis('square')
plt.show()

square axis barplot

相关问题