python - 如何创建漂亮的一维图?

标签 python pandas matplotlib seaborn

df = pd.DataFrame({"labels": ["This is a long label", 
                     "And this is also a long label", 
                     "Wow its a label thats pretty long", 
                     "Can you follow the pattern here?"],
                   "values": [2.2, 1.2, 3.0, 4.1]})

在真实示例中,我有大约 15 个标签和值。鉴于此,我想显示一个轴,标记为 2.2、1.2、3.0 和 4.1,每个轴都标有相应的标签。我的标签很长,所以我认为它们需要以对角线显示或使用其他类型的设备显示,以便标签保持清晰。

澄清

需要说明的是,我想要一个轴,没有图形字段。它会像一条数字线,上面有值的刻度线 -- 2.2、1.2 等。标签表示这些刻度线代表什么。

这里是这个想法的一个非常粗略的草图:enter image description here

我正在试验,但还没有找到做这个 pandas 或 seaborn 的方法。有可能吗?

最佳答案

绘制散点图,隐藏脊柱和 x 轴,使用注释功能放置文本。最后,调整定位直到对齐看起来正确

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.xaxis.set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.scatter([0]*len(df), df['values'], marker='d', s=100, c='r')
ax.set_xlim(-0.1,5)
for _, r in df.iterrows():
    ax.annotate(r['labels'], xy=(0.20, r['values']))

vertical number line

关于python - 如何创建漂亮的一维图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50661574/

相关文章:

python - Sobel 运算符的 Opencv 意外输出

python-2.7 - Mtaplotlib 属性错误 : 'module' object has no attribute 'pyplot'

python - 等同于 Python 中 R 的 createDataPartition

python - 如何在 matplotlib 中完全自定义的位置放置小刻度?

python - Python 中的删除方法

python - for语句(循环计算优先级)*不确定这是不是正确的术语

python - 如何将元素插入任意深度的空列表嵌套列表

python - 如何从用户那里获得交互式输入并在输入时能够使用箭头键?

python - 使用 np.where 根据某个值增加计数器

python - 由不同数据帧的唯一值组成的新数据帧