Skip to main content
 首页 » 编程设计

python之成对样本的带垂直直方图的平行轴点图

2025年05月04日17xiaohuochai

我有一些成对的连续数据,想将数据分成“箱”或大小相等的类别;然后使用 Python 和 MatplotLib 创建与所附图像类似的图。该图结合了平行线图以显示每个数据点的“之前”和“之后”值之间的差异,以及两个居中对齐的垂直直方图以显示每组中的数据分布:

上面的示例显然是使用 XLSTAT-PRO 完成的,但我一直无法在网络上找到可以使用 MatplotLib 或 Pandas 执行此操作的任何类似示例。

我打算自己尝试编写一个 Python/MatplotLib 例程,但想知道是否有人已经做过类似的事情?

如果有任何链接/帮助和建议,我将不胜感激。 感谢期待。

请您参考如下方法:

您可以开始阅读this tutorial .

以下是您可以针对您的问题进行调整的内容:

import matplotlib.pyplot as plt 
import numpy as np 
 
# your input data: 
befores = np.random.rand(10) 
afters = np.random.rand(10) 
 
# plotting the points 
plt.scatter(np.zeros(len(befores)), befores) 
plt.scatter(np.ones(len(afters)), afters) 
 
# plotting the lines 
for i in range(len(befores)): 
    plt.plot( [0,1], [befores[i], afters[i]], c='k') 
 
plt.xticks([0,1], ['before', 'after']) 
 
plt.show() 

输出: