我需要在 matplotlib 中绘制垂直散点图,但我在 matplotlib.org/examples 或 StackOverflow 中找不到任何内容。
我自己尝试了一些东西,但我缺少 Jitter。对于具有相同(或非常相似)Y 分量的点,抖动会稍微改变 X 分量,因此它们不会重叠。有什么我可以使用或者我必须手动更改 x 组件吗?
import numpy as np
from matplotlib import pyplot as plt
x = np.array([1,2,3])
l = ['A','B','C']
a = np.array([2,2,3])
b = np.array([3,3,4])
c = np.array([7,7,5])
d = (np.array(a) + np.array(b) + np.array(c)) / 3
plt.subplot(111)
plt.margins(0.2)
plt.xticks(x,l)
plt.plot(x, a, 'ro', label='a')
plt.plot(x, b, 'ro', label='b')
plt.plot(x, c, 'ro', label='c')
plt.plot(x, d, 'k_', markersize=15, label='avg')
plt.tight_layout()
plt.savefig('vertical_scatter')
plt.close()
这给了我关注
我在 Seaborn 上找到了这个.
这是我想要的,但只使用 matplotlib。
最佳答案
下面是一个仅使用 matplotlib 的抖动示例。这个想法基本上是向 x 值添加一些随机噪声。
import numpy as np
import matplotlib.pyplot as plt
data = np.random.rayleigh(scale=1, size=(30,4))
labels = list("ABCD")
colors = ["crimson", "purple", "limegreen", "gold"]
width=0.4
fig, ax = plt.subplots()
for i, l in enumerate(labels):
x = np.ones(data.shape[0])*i + (np.random.rand(data.shape[0])*width-width/2.)
ax.scatter(x, data[:,i], color=colors[i], s=25)
mean = data[:,i].mean()
ax.plot([i-width/2., i+width/2.],[mean,mean], color="k")
ax.set_xticks(range(len(labels)))
ax.set_xticklabels(labels)
plt.show()
关于python - 如何仅使用 matplotlib 绘制垂直散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982574/