【问题标题】:deprecated Warning - where parameter in mataplotlib library不推荐使用的警告 - mataplotlib 库中的 where 参数
【发布时间】:2020-10-17 05:23:17
【问题描述】:
Ages = [19,20,21,22,23,24,25,26,27]

python = [23000,30000,40000,50000,60000,70000,80000,90000,100000]
Java  = [15000,20000,25000,35000,40000,50000,90000,100000,300000]


pl.plot(Ages,python,label="Python developer") 
pl.plot(Ages,Java,label="Java developer") 

pl.fill_between(Ages,python,Java,where= (python > Java), alpha=0.5, color="blue" , label="greater than")
pl.fill_between(Ages,python,Java,where= (python < Java), alpha=0.5, color="red" , label="less than")


pl.title("Fill area on line plots")   
pl.legend(loc="upper left")
pl.show()

我收到弃用警告,当 python 值小于 Java 值时,我也没有得到红色边距。下面我提到了错误和输出屏幕截图...

错误:

matplot.py:143: MatplotlibDeprecationWarning: The parameter where must have the same size as x in fill_between(). This will become an error in future versions of Matplotlib.
  pl.fill_between(Ages,python,Java,where= (python > Java), alpha=0.5, color="blue" , label="greater than")
matplot.py:144: MatplotlibDeprecationWarning: The parameter where must have the same size as x in fill_between(). This will become an error in future versions of Matplotlib.
  pl.fill_between(Ages,python,Java,where= (python < Java), alpha=0.5, color="red" , label="less than")

输出:

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    此弃用警告主要是指出where 参数与其他数组的参数数量不同。通常它暗示不受欢迎的行为。之前,此类错误被忽略。

    where 参数预计对于绘图上的每个点都有一个 True/False 值。

    在这种情况下,表达式 python &lt; Java 只有一个值,因为两者都是常规 Python 列表。要获取单个布尔值的列表,您需要手动创建该列表[p &lt; j for p, j in zip(python, Java)]。或者将所有内容更改为 numpy 数组,并让其 vectorization and broadcasting 负责创建布尔值数组。

    请注意,您需要一个额外的参数interpolate=True 来处理其中一个点的值为True 而另一个点的值为False 的线段。

    这是将列表更改为 numpy 数组(python &gt; Java 然后也返回一个数组)和 interpolate=True 设置的代码。

    import matplotlib.pyplot as plt
    import numpy as np
    
    Ages = np.array([19, 20, 21, 22, 23, 24, 25, 26, 27])
    
    python = np.array([23000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000])
    Java = np.array([15000, 20000, 25000, 35000, 40000, 50000, 90000, 100000, 300000])
    
    plt.plot(Ages, python, label="Python developer")
    plt.plot(Ages, Java, label="Java developer")
    
    plt.fill_between(Ages, python, Java, where=(python > Java), interpolate=True,
                     alpha=0.5, color="blue", label="greater than")
    plt.fill_between(Ages, python, Java, where=(python < Java), interpolate=True,
                     alpha=0.5, color="red", label="less than")
    plt.title("Fill area on line plots")
    plt.legend(loc="upper left")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 2020-01-31
      • 2021-10-09
      • 2020-05-14
      • 2019-10-11
      • 2016-09-19
      • 2015-02-26
      • 2017-03-01
      • 1970-01-01
      相关资源
      最近更新 更多