matplotlib控制颜色

时间:2017-05-23 13:03:34

标签: python python-3.x matplotlib

我正在绘制这个数据文件,工作正常。

def orb_p():
  #  print(data[:,0])
  pxp = data[:,3]
  pxm = data[:,4]
  pyp = data[:,5]
  pym = data[:,6]
  pzp = data[:,7]
  pzm = data[:,8]
  plt.plot(x,pxp, "-")
  plt.plot(x,pxm, "-")
  plt.plot(x,pyp, "-")
  plt.plot(x,pym, "-")
  plt.plot(x,pzp, "-")
  plt.plot(x,pzm, "-")
  plt.fill_between(x,pxp)
  plt.fill_between(x,pyp)
  plt.fill_between(x,pzp)
  plt.fill_between(x,pxm)
  plt.fill_between(x,pym)
  plt.fill_between(x,pzm)
  plt.show()

问题是我想为pxp和pxm使用相同的颜色(对于(pyp,pym)和(pzp,pzm)相同)。我可以使用(r-)等选项手动控制它们,但我想使用默认颜色方案(ggplot)。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

默认情况下,// you may also declare it as public class ValueGetter<T extends A, R>, // but there would be no actual benefit... public final class ValueGetter<T, R> { private String name; private Function<T,R> getter; private ValueGetter(String name, Function<T, R> getter){ this.name = name; this.getter = getter; } public static final ValueGetter<A, Integer> AA = new ValueGetter<>("a", A::getA); public static final ValueGetter<B, String> BB = new ValueGetter<>("b", B::getB); public Function<T, R> getGetter() { return this.getter; } public String name() { return name; } } 不应推进颜色循环器。因此,在相应fill_between之后直接绘制相应的fill_between应该为它们提供相同的颜色。

plot

enter image description here

请注意,您实际上并不需要import numpy as np; np.random.seed(1) import matplotlib.pyplot as plt x = np.linspace(0,2, 23) data = np.fliplr(np.cumsum(np.cumsum(np.random.normal(size=(len(x),9)), axis=0)+3, axis=1)) for i in range(3,9): plt.plot(x,data[:,i], "-") plt.fill_between(x,data[:,i]) plt.show() 功能。离开它会产生相同的结果。enter image description here

虽然我不知道为什么会这样,但如果plot不能推进颜色循环器。; - )

答案 1 :(得分:0)

要访问色轮中使用过的颜色:

l, = plt.plot(x)
color = l.get_color()

我没有为fill_between尝试过,但我认为您可能需要

l = plt.fill_between(...)
color = l.get_cmap()