根据值填充颜色?

时间:2016-04-30 11:39:21

标签: python matplotlib

我在Python / matplotlib / pandas中寻找一种方法来为类似于此的图形创建颜色填充(来源:http://www.scminc.com/resources/SCM_TIPSTRICKS_Petrel_Well_Sections_2013_July14.pdf):

enter image description here

它使用填充颜色贴图(图像的左侧),并根据x轴上的特定间隔为其指定颜色。不幸的是,我还没有找到解决方案,而且由于我对Python很新,所以我无法找到办法。

非常感谢

1 个答案:

答案 0 :(得分:3)

您可以使用imshow将填充作为背景绘制,然后剪辑。您可以使用fill_betweenx制作面具。

以下是使用随机数据的示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch

# Make a random x and a y to go with it.
np.random.seed(26)
x = np.random.normal(0, 1, 200).cumsum()
y = np.arange(x.size)

# Set up the figure.
fig, ax = plt.subplots(figsize=(2, 10))

# Make the background 'image'.
im = ax.imshow(x.reshape(-1, 1),
               aspect='auto',
               origin='lower',
               extent=[x.min(), x.max(), y.min(), y.max()]
              )

# Draw the path.
paths = ax.fill_betweenx(y, x, x.min(),
                         facecolor='none',
                         lw=2,
                         edgecolor='b',
                        )

# Make the 'fill' mask and clip the background image with it.
patch = PathPatch(paths._paths[0], visible=False)
ax.add_artist(patch)
im.set_clip_path(patch)

# Finish up.
ax.invert_yaxis()
plt.show()

这会产生:

some random data filled with colour