基于值的颜色填充

基于值的颜色填充

本文介绍了基于值的颜色填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Python/matplotlib/pandas 中寻找一种方法来为与此类似的图形创建颜色填充(来源:

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

非常感谢

解决方案

您可以使用 imshow 将填充绘制为背景,然后进行裁剪.您可以使用 fill_betweenx 来制作蒙版.

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

将 numpy 导入为 np导入matplotlib.pyplot作为plt从 matplotlib.patches 导入 PathPatch# 随机生成一个 x 和一个 y.np.random.seed(26)x = np.random.normal(0,1,200).cumsum()y = np.arange(x.size)#设置数字.无花果,ax = plt.subplots(figsize =(2,10))#将背景设为图片".im = ax.imshow(x.reshape(-1,1),方面='自动',origin ='lower',范围= [x.min(),x.max(),y.min(),y.max()])# 绘制路径.路径= ax.fill_betweenx(y,x,x.min(),facecolor='无',lw = 2,edgecolor ='b',)#制作填充"蒙版,并用其剪切背景图像.patch = PathPatch(paths._paths[0],可见=假)ax.add_artist(补丁)im.set_clip_path(补丁)# 完事.ax.invert_yaxis()plt.show()

这产生:

I am looking for a way in Python/matplotlib/pandas to create a color fill for a graph similar to this (Source: http://www.scminc.com/resources/SCM_TIPSTRICKS_Petrel_Well_Sections_2013_July14.pdf):

It uses a color map for the fill (left of the image), and based on a specific interval on the x-axis assigns a color to it. Unfortunately, I haven't found a solution, and since I am pretty new to Python in general, I am unable to find a way to do that.

Many thanks

解决方案

You can plot the fill as a background with imshow, then clip it. You can use fill_betweenx to make the mask.

Here's an example using random data:

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()

This yields:

这篇关于基于值的颜色填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:46