基于数值的颜色填充?

2024-09-28 23:16:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Python/matplotlib/pandas中寻找一种方法,为类似于以下内容的图形创建颜色填充(Source:http://www.scminc.com/resources/SCM_TIPSTRICKS_Petrel_Well_Sections_2013_July14.pdf):

enter image description here

它将颜色贴图用于填充(图像左侧),并根据x轴上的特定间隔为其指定颜色。不幸的是,我还没有找到解决方案,而且由于我对Python还不太熟悉,所以我无法找到一种方法来实现这一点。在

非常感谢


Tags: 方法comhttp图形sourcepandasmatplotlib颜色
1条回答
网友
1楼 · 发布于 2024-09-28 23:16:35

您可以使用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

相关问题 更多 >