2016-04-30 116 views
4

我正在尋找在Python/matplotlib /大熊貓一種方式來創建一個顏色填充了一個類似的圖(來源:http://www.scminc.com/resources/SCM_TIPSTRICKS_Petrel_Well_Sections_2013_July14.pdf):基於值的顏色填充?

enter image description here

它採用了彩色地圖的填充(左),並根據x軸上的特定間隔爲其分配顏色。不幸的是,我還沒有找到解決方案,因爲我對Python一般都很陌生,所以我無法找到解決方法。

非常感謝

+1

好,如果你不是絕對必須有一定範圍的顏色,你可以使用[fill_between] (http://matplotlib.org/examples/pylab_examples/fill_between_demo.html)。否則,你可能需要類似[補丁集合](http://matplotlib.org/examples/api/patch_collection.html)。你也可以考慮用一個補丁製作一個矩形圖像並[剪切它](http://matplotlib.org/examples/images_contours_and_fields/image_demo_clip_path.html)。 –

+0

..繼續之前的評論...對於最終選項,您需要使用[Polygon patch](http://matplotlib.org/api/patches_api.html#matplotlib.patches.Polygon)。 –

+0

'pcolor'也可能適用於此。 – tacaswell

回答

0

您可以繪製填充與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