python - 如何仅使用 matplotlib 绘制垂直散点图

标签 python matplotlib plot

我需要在 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()

这给了我关注

enter image description here

我在 Seaborn 上找到了这个.

enter image description here

这是我想要的,但只使用 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()

enter image description here

关于python - 如何仅使用 matplotlib 绘制垂直散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982574/

相关文章:

python - 元组python中的最大第二个元素

python - 如何在骰子游戏中的正确位置结束 try 循环?

python - 为什么我的函数会在函数范围之外更改其参数?

datetime - 将 pandas 多索引转换为 pandas 时间戳

python - 如何选择列中具有最大值的数据框中的行

python - SQLAlchemy SessionTransaction 有查询属性吗?

python - 如何使用输入 *.txt 文件绘制一个非常简单的条形图(Python、Matplotlib)?

python - 使用带有 MacOSX 后端的 python matplotlib 设置图形的绝对位置

r - 增加情节刻度线的长度

python - 与 pandas 绘图 : group and mean