基于值的颜色填充?

2024-04-02

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

它使用颜色图进行填充(图像左侧),并根据 x 轴上的特定间隔为其分配颜色。不幸的是,我还没有找到解决方案,而且由于我对 Python 总体来说还很陌生,所以我无法找到一种方法来做到这一点。

非常感谢


您可以将填充绘制为背景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()

这产生:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于值的颜色填充? 的相关文章

随机推荐