我正在尝试在 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()
非常感谢!
答案 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
位置。使用 x
将 np.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()