【问题标题】:Matplotlib line plot of x values against yx 值对 y 的 Matplotlib 线图
【发布时间】:2015-04-14 14:58:58
【问题描述】:

我有一个值 x 的列表,我想在 y 轴上绘制(用线)。

也就是说,如果 x = [3, 5, 2, 4] 和 y = ['A', 'B', 'C', 'D'],情节可能看起来像这样(除了不是手-绘制):

恐怕我在 matplotlib.pyplot docs / SO / google 中没有看到任何指向正确方向的内容,甚至没有看到它的名称。有什么指点吗?

【问题讨论】:

  • 我不知道这是否可能,但我建议绘制 y 轴而不是 x 轴。否则,您将违背人们通常理解图表的方式。
  • 考虑绘制树干上的苔藓密度(作为高度的函数)。有时在 y 轴上作图更直观!
  • 我不确定是否直接这样做,但您可以轻松地将字符串映射为整数,然后更改刻度标签。

标签: python python-3.x matplotlib


【解决方案1】:

我认为您正在寻找类似的东西

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots(1)

# create some x data and some integers for the y axis
x = np.array([3,5,2,4])
y = np.arange(4)

# plot the data
ax.plot(x,y)

# tell matplotlib which yticks to plot 
ax.set_yticks([0,1,2,3])

# labelling the yticks according to your list
ax.set_yticklabels(['A','B','C','D'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多