Luc*_*ian 5 python matplotlib seaborn
如果我们绘制非常简单的直方图:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
Z = np.array([1, 2, 2, 2, 2, 2, 2, 3, 4, 5])
sns.histplot(Z, bins = 5)
plt.xticks([1, 2, 3, 4, 5])
plt.show()
Run Code Online (Sandbox Code Playgroud)
我们获得:
我的问题是 - 是否有可能使这些刻度相对于 bin 居中?即我想1在第一个垃圾箱的中间打勾,2在第二个垃圾箱的中间打勾,即
你知道这是否可能吗?
正如评论中所建议的,我使用了discrete = True并且有效。但是,我不明白为什么它在创建子图时会刹车:
sns.set(rc={'figure.figsize':(10,15)})
X = np.array([1, 1, 1, 1, 1, 1, 2, 3, 4, 5])
Y = np.array([1, 2, 3, 4, 5, 5, 5, 5, 5, 5])
Z = np.array([1, 2, 2, 2, 2, 2, 2, 3, 4, 5])
f, axes = plt.subplots(1, 3)
sns.histplot(X, color = "b", ax = axes[0], discrete = True)
plt.xticks(np.arange(1, 6))
sns.histplot(Y, color = "r", ax = axes[1])
plt.xticks([1, 2, 3, 4, 5])
sns.histplot(Z, bins = 5, color = "g", ax = axes[2])
plt.xticks([1, 2, 3, 4, 5])
plt.show()
Run Code Online (Sandbox Code Playgroud)
你知道为什么会这样吗?
| 归档时间: |
|
| 查看次数: |
2458 次 |
| 最近记录: |