繁体   English   中英

如何保持最后绘制的line2D始终是相同的颜色?

[英]How to keep the last plotted line2D always the same color?

我正在编写一个程序,用户可以在定义的轴上绘制函数。 有没有办法使最后绘制的函数始终是固定的颜色(让我们说绿色)?

例如,下面的代码将多项式的次数作为输入,并绘制相同和较低程度的所有多项式。 我想要一个调整,如最后一个情节(在这种情况下,最高度的多项式)总是绿色:

import numpy as np
import matplotlib.pyplot as plt

def plot_polynomials(highest_degree):

    x = np.arange(0,1,0.01)
    for degree in np.arange(1,highest_degree+1):
        coefficients = np.repeat(1,degree)
        label = 'degree={}'.format(degree)
        polynomial = np.polynomial.polynomial.polyval(x, coefficients)
        plt.plot(x, polynomial, label=label)

    plt.legend()
    plt.show()

plot_polynomials(6)

期待评论!

这应该做:

def plot_polynomials(highest_degree):

    x = np.arange(0,1,0.01)
    for degree in np.arange(1,highest_degree+1):
        coefficients = np.repeat(1,degree)
        label = 'degree={}'.format(degree)
        colors=plt.rcParams['axes.prop_cycle'].by_key()['color']
        colors.pop(2) #Removing green from color cycle
        polynomial = np.polynomial.polynomial.polyval(x, coefficients)
        if degree==highest_degree:
            plt.plot(x, polynomial, label=label, color='g', lw=3)
        else:
            plt.plot(x, polynomial, label=label, color=colors[degree-1])
    plt.legend()
    plt.show()

plot_polynomials(6)

输出:

在此输入图像描述

注意:用lw做粗线,但这显然是可选的

编辑:从颜色循环中删除绿色,因此只有一条绿线

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM