【问题标题】:How to change line colors succesively in a graph using matplotlib如何使用 matplotlib 在图形中连续更改线条颜色
【发布时间】:2018-04-30 18:56:15
【问题描述】:

所以我绘制了这条线

x=0.2  
r_1=3
n=1000

for i in range (n):
        x_n1=r_1*x*(1 -x)
        plt.plot([x, x], [x, x_n1], color='green')
        plt.plot([x, x_n1], [x_n1, x_n1], color='green')
        x=x_n1
plt.show()

它们都是绿色的。我怎样才能让他们每个人在每次迭代中改变它的颜色(如颜色的彩虹顺序)?我想要实现的是这样的cobweb map

【问题讨论】:

    标签: python matplotlib graph colors


    【解决方案1】:

    color 接受多种类型的输入,例如 RGB 值的元组。但是,创建彩虹的最佳方法是使用HSV。幸运的是,matplotlib.colors 有一个hsv_to_rgb 函数:

    import matplotlib.colors as colors
    color = .5 #Value between 0 and 1, 0 and 1 being red
    print(colors.hsv_to_rgb((color, 1, 1)) #[ 0.  1.  1.]
    

    现在,我们只想在从 0 迭代到 n-1 时转换所有值:

    for i in range (n):
        color=colors.hsv_to_rgb((i/n, 1, 1))
        x_n1=r_1*x*(1 -x)
        plt.plot([x, x], [x, x_n1], color=color)
        plt.plot([x, x_n1], [x_n1, x_n1], color=color)
        x=x_n1
    plt.show()
    

    您会注意到几乎所有的线条都是微红色的。这是因为大多数流行的行是前 50 行,它们都非常接近红色。您可以根据需要进行调整;一种这样的选择是拥有color=colors.hsv_to_rgb(((i/n)**(1/2), 1, 1))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多