【问题标题】:Matplotlib fill betweenMatplotlib 之间填充
【发布时间】:2014-04-05 21:44:56
【问题描述】:

我想在此图中使用填充或任何必要的颜色而不是绘制点。基本上用连续的色带代替点。我怎样才能做到这一点?

相关代码如下(点可以认为是三组实验数据,或多或少)

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from pylab import figure, show

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

fig = figure()
ax = fig.add_subplot(111)

p1, = ax.plot(x_124,y_124,'o')
p2, = ax.plot(x_125,y_125,'o')
p3, = ax.plot(x_126,y_126,'o')

xmax=max(x_124+x_125+x_126)
xmin=min(x_124+x_125+x_126)
ymax=max(y_124+y_125+y_126)
ymin=min(y_124+y_125+y_126)

plt.title('Escenario $m_{h}^{\mathrm{max}}$')

plt.ticklabel_format(style='sci',scilimits=(0,0),axis='both')

ax.xaxis.set_major_locator(MaxNLocator(12))
ax.yaxis.set_major_locator(MaxNLocator(12))
plt.xlim(0.96*xmin,1.04*xmax)
plt.ylim(0.96*ymin,1.04*ymax)

plt.legend(['$m_h$ = 124 GeV','$m_h$ = 125 GeV','$m_h$ = 126 GeV'],numpoints=1,loc=0)

plt.xlabel('M3SQ / GeV')
plt.ylabel('M3SU / GeV')

ax.grid()
show()

【问题讨论】:

  • 你能展示你用来得到这个数字的代码吗?
  • 您能解释一下“连续色带”是什么意思吗?举个例子就好了。
  • 我不确定我的确切意思是什么,就像填充颜色的点之间的空白空间,而不仅仅是点。

标签: python matplotlib


【解决方案1】:

我知道这是一个不完整的答案,应该有更优雅的方法来做到这一点。但它应该是一种快速而肮脏的方法,至少对于你图形的右上角是这样。

对于每个 m_h,

  • 遍历所有 x 值,
  • 并查找该特定 x 值(针对该 m_h)的最高和最低 y 值。
  • 完成所有 x 值后,您将获得 3 个列表,以便在 matplotlib 中使用 fill_between 函数。

当然,通过这种方式,您必须决定如何连接不同的“连续颜色带”,例如右上角的红色和绿色。

在您的绘图中使用给定数量的“点”时,此方法不适用于中间的一组点(图中大致从 y=1.6 到 x=1.6 的点)。这是因为您无法从一系列一维点构造带。您需要更多样品。

【讨论】:

  • 嗨,Pawin,谢谢。我终于使用了颜色图和插值,但谢谢:)