【问题标题】:x and y are not the same size for graphing. And need text inside graphx 和 y 的绘图大小不同。并且需要图表内的文字
【发布时间】:2016-08-08 19:39:37
【问题描述】:
import random
import matplotlib.pyplot as plt

num1 = 30
num3 = [4000,3000,1500,9000,2500,8000,1200,800,900,1000,5400,9500,1100,3400,8100,
        5500,1200,3830,2311,9999]
#num3_array = []
num2_array = []
for i in range (0,20):
    num2 = random.randrange(0,45)
    print(num2)

#num3_array.append(num3)
num2_array.append(num2)

plt.axvline(num1,0,color="r")
plt.scatter(num2_array,num3,marker=",")

我想在 x 轴上有 num2 值,在 y 轴上有相应的 num3 值。我不知道如何得到它。我不断收到错误消息。

ValueError: x and y must be the same size

我知道这意味着什么。我只是不知道如何解决它。

另外,我需要图表内的文本。我想指着红线说“num1 = 30”,然后数出红线左右两边散落的点的个数,把数放在右下角图形。在红线上被认为是在右手边。

上图是我用excel创建的。我需要的是使用 matplotlib 在 RHS 角落的那个盒子。该框包含 LHS 和 RHS 上的点数。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我对这个程序所做的第一个更改是将 y 值的长度与“x”值的长度相匹配,这可以让您开始绘图:

    num2 = []
    for i in range (len(num3)):
        num2.append(random.randrange(0,45))
        print(num2)
    plt.plot(num2, num3)
    plt.show()
    

    【讨论】:

    • 谢谢!这是其中的一部分。我将“plt.plot”更改为“plt.scatter”,因为那是显示点的内容。现在,下一部分呢?
    • 这里有一个链接,解释了更多关于 matplotlib 中的注释:[链接]matplotlib.org/users/annotations_intro.html
    • 我猜是这样的一行:plt.annotate(num1, xy=(num1, 11000), xytext=(10, 5), arrowprops=dict(facecolor='black', shrink=0.05), ) 但我想要 "num 1 = 30" 应该从 num1 = 读取 30。而且我不希望箭头这么长。只想要一个干净的图像。而不是说“11000”,我只是希望它几乎在顶部,但不接触。那么计数呢?
    • 如果它会太乱,那么不要担心注释部分。我只想要计数。
    【解决方案2】:

    为了从xy 坐标迭代绘制散点图,这两个迭代必须具有相同的长度。如果 x 可迭代对象比 y 长,那么应该给出额外的 x 值的 y 坐标是多少(反之亦然)?

    import random
    import matplotlib.pyplot as plt
    
    num1 = 30
    num3 = [4000,3000,1500,9000,2500,8000,1200,800,900,1000,5400,9500,1100,3400,8100,
            5500,1200,3830,2311,9999]
    
    num2_array = []
    for _ in num3:
        num2 = random.randrange(0,45)
        print(num2)
        num2_array.append(num2)
    
    plt.axvline(num1,0,color="r")
    plt.scatter(num2_array,num3,marker=",")
    plt.show()
    

    现在可以正常工作了,但是 numpy 使用 numpy.random.random_integers 函数为我们提供了一种更好(更快、更清晰等)的方法。

    import random
    import matplotlib.pyplot as plt
    import numpy as np
    
    num1 = 30
    num3 = [4000,3000,1500,9000,2500,8000,1200,800,900,1000,5400,9500,1100,3400,8100,
            5500,1200,3830,2311,9999]
    
    num2_array = np.random.random_integers(0,45,len(num3))
    
    plt.axvline(num1,0,color="r")
    plt.scatter(num2_array,num3,marker=",")
    plt.show()
    

    要计算红线左侧的点,您可以简单地执行以下操作:

    count = 0
    for value in num2_array:
        if value <= num1:
            count += 1
    

    要在绘图上写下这个计数,您需要查看 matplotlib 的 annotating plots 的各种方法。您可以使用 text 方法绘制计数文本,并告诉 matplotlib 使用轴坐标系,使其始终绘制在左上角。您还需要 annotate 函数来使用箭头绘制注释。下面是一个完整的例子

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    num1 = 30
    num3 = [4000,3000,1500,9000,2500,8000,1200,800,900,1000,5400,9500,1100,3400,8100,
            5500,1200,3830,2311,9999]
    
    num2_array = np.random.random_integers(0,45,len(num3))
    
    count = 0
    for value in num2_array:
        if value <= num1:
            count += 1
    
    ax.axvline(num1,0,color="r")
    ax.scatter(num2_array,num3,marker=",")
    
    ax.text(x=0.85,y=0.95,s="Count: {}".format(count), transform=ax.transAxes)
    ax.annotate("Text", xy=(num1, max(num3)/2), xytext=(num1*1.2, (max(num3)/2)*1.2), arrowprops=dict(facecolor='black'))
    plt.show()
    

    【讨论】:

    • 我需要使用 matplotlib。你能帮我做下一部分吗?
    • 让我们只关心计数。你的代码给了我 0 个计数。
    • @BobH。您是否完全按照我在最终“代码块”中的代码复制了代码?对我来说,它给了this
    • 是的,我有。老实说,我只能使用 matplotlib,因为我拥有的原始代码是结构化的,如果我想绘制一些东西,那么它必须是 matplotlib。我的原始代码比这个复杂得多。我真的很感谢你的努力。你在这里做了很多。如果您将其调整为仅 matplotlib,那就太好了!我试过了,但我没有得到它,因为我是 python 新手,所有其他项目的插图都不是关键,所以从来没有真正使用过它。现在,由于我无法将它用于 matplotlib,我只是想在图表下方进行计数。
    • 你的 num1 和 num3 没问题,但是 num2 是使用 numpy 的,这对我不起作用。
    猜你喜欢
    • 2018-12-14
    • 2015-09-11
    • 2019-06-02
    • 2021-12-02
    • 2014-05-06
    • 2017-11-02
    • 2022-01-12
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多