【问题标题】:Matplotlib: Changing the colour of the line after certain point in indexMatplotlib:在索引中的某个点之后更改线条的颜色
【发布时间】:2017-05-16 10:42:22
【问题描述】:

我已经探索 Matplotlib 和 pandas 一个多月了,我想知道是否有任何方法可以在通过某个索引值后更改给定图形线的颜色,我可以'在 Matplotlib 文档中找不到任何类似的东西,非常感谢任何人的专业知识。

ax1 = plt.subplot2grid((1,1), (0,0))

num = 0
for i in df['GDP']:
    num +=1
    if num > 263:
        df.plot(color = 'r', ax=ax1)
    else:
        df.plot(color= 'r', ax=ax1)
plt.show()

我认为这样的事情会奏效,但幸运的是它没有。

注意:如果有人想知道的话,我正在使用 GDP 数据,这是我数据框中唯一的一列:)

【问题讨论】:

    标签: python pandas matplotlib colors


    【解决方案1】:

    您可以用loc 覆盖boolean indexing 的行,用于选择列GDP,其中比较索引值(index 必须单调递增,默认):

    np.random.seed(100)
    df = pd.DataFrame(np.random.randint(10, size=(30,2)), columns=['GDP', 'A'])
    #print (df)
    
    N = 20
    ax = df['GDP'].plot(color='y')
    df.loc[df.index >= N, 'GDP'].plot(color='r', ax=ax)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      相关资源
      最近更新 更多