【问题标题】:how can I plot on the axes如何在轴上绘图
【发布时间】:2015-07-14 07:41:01
【问题描述】:

我实际上想重新创建如下图像:

特别是 x 轴上的小 X 我有一个

list = [[100,-3],[200,None],[120,-2] ... ]

我愿意

for x in list:
     if x[1]!=None:
          plot(x[0],x[1],'ok')
     else:
          ###  PLot on the axes ###

但是当我在绘图时,我不知道轴是什么。我知道有些值是无,例如 (250,None),所以我想在 x = 250 的 x 轴上绘图,但我不知道 min(ylim()) 最终会是什么。

我知道我可以做到plot(250,-5,'X',zorder=999999) 但这只是当我知道最小轴是什么时..(我不能做 min、max 等来知道 min 轴。因为真实数据是字典内的列表内的列表等.. )

【问题讨论】:

  • 添加你尝试过的代码
  • 您是在问如何在轴脊(x 轴线)上绘图吗?或者您想精确地绘制在线上(即将点设置在线上而不是指定 y 坐标)?
  • @hitzg 是的,我想在线绘制,而不是指定 y 坐标

标签: python matplotlib axes


【解决方案1】:

所以诀窍是使用自定义转换。 x 轴的常规数据转换和 y 轴的轴转换。 Matplotlib 将其称为混合转换,您需要自己创建。您可以在此awesome guide 中找到更多信息。

正如@ThePredator 已经指出的那样,您必须设置clip_on=False,否则您的标记将被剪裁。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

fig, ax = plt.subplots()

# the x coords of this transformation are data, and the
# y coord are axes
trans = transforms.blended_transform_factory( ax.transData, ax.transAxes)

# data points on the axes
x = np.random.rand(5)*100. + 200.
y = [0]*5
ax.plot(x, y, 'kx', transform=trans, markersize=10, markeredgewidth=2,
        clip_on=False)

# regular data
x = np.random.rand(5)*100. + 200.
y = np.random.rand(5)*100. + 200.
ax.plot(x, y, 'ro')

plt.show()

结果:

【讨论】:

    【解决方案2】:

    您可以使用clip_on = False 选项。示例:

    在您的情况下,您可以设置 y 限制。

    例子:

    x = [0,1,2,3,4,5]
    y = [0,0,0,0,0,0]
    
    plt.plot(x,y,'x',markersize=20,clip_on=False,zorder=100)
    plt.ylim(0,1)
    plt.show()
    

    【讨论】:

    • 但这只是在我知道轴的情况下。
    【解决方案3】:

    您可以使用get_ylim() 来获取轴的位置,然后在其上绘图。

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多