python - 不从零/显示范围开始的水平条形图

标签 python matplotlib graph

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

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

抱歉,我无法提供示例,因为到目前为止我什么也没得到。条形图不能满足我的要求。

编辑:我基本上想要类似 this example 的东西但没有实际的条形图,并且能够输入我的数据作为误差条。据我所知,误差线只能处理与“主要数据”相关的错误。

最佳答案

如果我理解正确的话,你可以用一个简单的误差条图来做到这一点(尽管它有点像黑客):

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

关于python - 不从零/显示范围开始的水平条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33827214/

相关文章:

python - 如何根据一系列 if\else 条件和匹配值从多个数据帧中 BEST 提取信息? (需要指导!))

python - For 循环在 matplotlib 中绘制零 [Python]

python - 如何按升序对条形图中的条形进行排序

python - 用户鼠标在绘图上输入积分限制 - python

r - 图形帮助 - 圆形条形图

python - 如何将 CSV 读入 Python 字典,其中每个键的值都是字典列表?

Python unpickle 一个对象里面有一个类实例

Python进程同步

algorithm - 关于图中两个节点之间的最短路径的声明?

Python MatPlot 条函数参数