不从零开始/显示范围

时间:2015-11-20 12:52:37

标签: python matplotlib graph

我试图想象一下我拥有的大约20个样本的一组频率范围。我想要做的是一个水平条形图,其中每行代表一个样本。样本名称应该在左侧和右侧,我想要一个限制为0和150 kHz的x轴。

现在我的范围是(70.5,95.5)。我可以用水平条形图来实现这一点,还是在寻找不同类型的图表?

很抱歉,我无法提供一个例子,因为到目前为止我什么都没有。条形图只是没有做我想要的。

编辑:我基本上想要this example中的内容,但没有实际的条形,并且能够为错误栏输入我的数据。据我所知,误差线只能处理相对于“主数据”的错误。

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你可以用一个简单的错误栏图表来做到这一点(虽然它有点像黑客):

import numpy as np
import matplotlib.pyplot as plt

# 20 random samples
nsamples = 20
xmin, xmax = 0, 150
samples = np.random.random_sample((nsamples,2)) * (xmax-xmin) + xmin
samples.sort(axis=1)
means = np.mean(samples, axis=1)
# Find the length of the errorbar each side of the mean
half_range = samples[:,1] - means
# Plot without markers and customize the errorbar
_, caps, _ = plt.errorbar(means, np.arange(nsamples)+1, xerr=half_range, ls='',
                          elinewidth=3, capsize=5)
for cap in caps:
    cap.set_markeredgewidth(3)

# Set the y-range so we can see all the errorbars clearly
plt.ylim(0, nsamples+1)
plt.show()

enter image description here