If you'd like to explicitly specify the colors that will be used, just pass it to the color kwarg (html colors names are accepted, as are rgb tuples and hex strings):import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 1, 10)for i, color in enumerate(['red', 'black', 'blue', 'brown', 'green'], start=1): plt.plot(x, i * x + i, color=color, label='$y = {i}x + {i}$'.format(i=i))plt.legend(loc='best')plt.show()最后,如果您想从现有颜色图中自动选择指定数量的颜色:import matplotlib.pyplot as pltimport numpy as npx = np.linspace(0, 1, 10)number = 5cmap = plt.get_cmap('gnuplot')colors = [cmap(i) for i in np.linspace(0, 1, number)]for i, color in enumerate(colors, start=1): plt.plot(x, i * x + i, color=color, label='$y = {i}x + {i}$'.format(i=i))plt.legend(loc='best')plt.show() 这篇关于在 matplotlib 中绘制不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:59