我想在一个情节中绘制很多图表。我刚刚开始使用matplotlib并且无法找到一种很好的方法来生成许多可区分的颜色:(可能最多使用SV在HSV上骑行?
我在考虑像
这样的东西args=[]
for i,(x,y) in enumerate(data):
args.extend([x,y,hsv(i)])
plot(*args)
有什么建议吗? :)
答案 0 :(得分:26)
我认为你有正确的想法,但如果你传递的颜色图hsv
数字在范围(0,1)上展开,颜色会更加明显:
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()