matplotlib用于绘图的大颜色集

2024-06-13 22:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个图中画出很多图。我刚从matplotlib开始,找不到一个好的方法来生成许多可分辨的颜色:(可能在HSV上循环,最大值是SV?

我在想

args=[]
for i,(x,y) in enumerate(data):
    args.extend([x,y,hsv(i)])
plot(*args)

有什么建议吗?:)


Tags: 方法infordataplotmatplotlib颜色args
1条回答
网友
1楼 · 发布于 2024-06-13 22:26:41

我认为您的想法是正确的,除了如果您传递分布在范围(0,1)上的colormaphsv数字,颜色将更容易区分之外:

hsv = plt.get_cmap('hsv')
hsv(float(i)/(len(data)-1))

或者,使用NumPy:

colors = hsv(np.linspace(0, 1.0, len(kinds)))

例如:

import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import scipy.interpolate as interpolate

dates = [DT.date(year, 9, 1) for year in range(2003, 2009)]
t = list(map(mdates.date2num, dates))
jec = (100, 70, 125, 150, 300, 250)
plt.plot(dates, jec, 'k.', markersize = 20)
new_t = np.linspace(min(t), max(t), 80)
new_dates = map(mdates.num2date, new_t)
kinds = ('cubic', 'quadratic', 'slinear', 'nearest', 'linear', 'zero', 4, 5)
cmap = plt.get_cmap('jet')
colors = cmap(np.linspace(0, 1.0, len(kinds)))
for kind, color in zip(kinds, colors):
    new_jec = interpolate.interp1d(t, jec, kind=kind)(new_t)
    plt.plot(new_t, new_jec, '-', label=str(kind), color=color)
plt.legend(loc = 'best')
plt.show()

enter image description here

相关问题 更多 >