添加带有条件着色的水平线

时间:2019-05-06 08:00:56

标签: python matplotlib

我使用Accept绘制了accept图。现在,我想有一条水平线(或者类似do you want to resubmit this form的东西也行),并在contourf处进行条件着色。我将向您展示我所拥有的以及我想要获得的。我想用一个数组来做,比如说matplotlib.pyplot,它代表陆地,海洋或冰。该数组充满了ax.vspan(陆地),y = 0(海洋)或landsurface(冰),并具有1(即x轴)。

这是情节代码:

2

这是我到目前为止所拥有的:

Original

这就是我想要的:

Edited

非常感谢!

2 个答案:

答案 0 :(得分:1)

简介

我将使用line collection

enter image description here 因为我没有您的原始数据,所以我使用简单的正弦曲线伪造了一些数据,并在基线上绘制了与曲线的小,中和高值相对应的颜色代码

代码

通常情况下,我们需要显式导入LineCollection

import matplotlib.pyplot as plt                                                  
import numpy as np                                                               
from matplotlib.collections import LineCollection

只需画出正弦曲线(x r

x = np.linspace(0, 50, 101)                                                      
y = np.sin(0.3*x)

从曲线值(对应于您的表面类型)到LineCollection颜色的颜色编码,请注意LineCollection 需要将颜色指定为RGBA元组,但是我看过使用颜色字符串的示例,ba!

# 1 when near min, 2 when near 0, 3 when near max
z = np.where(y<-0.5, 1, np.where(y<+0.5, 2, 3))                                  

col_d = {1:(0.4, 0.4, 1.0, 1), # blue, near min
         2:(0.4, 1.0, 0.4, 1), # green, near zero
         3:(1.0, 0.4, 0.4, 1)} # red, near max                     
# prepare the list of colors
colors = [col_d[n] for n in z]                                                   

在一个行集合中,我们需要一个段序列,在这里,我决定将我的编码行放置在y=0上,但是您只需在s上添加一个常数即可上下移动它。 br /> 我承认形成分段序列有点棘手...

# build the sequence of segments
s = np.zeros(101)                                                                
segments=np.array(list(zip(zip(x,x[1:]),zip(s,s[1:])))).transpose((0,2,1))       
# and fill the LineCollection
lc = LineCollection(segments, colors=colors, linewidths=5, 
                    antialiaseds=0, # to prevent artifacts between lines 
                    zorder=3        # to force drawing over the curve)                                                                 lc = LineCollection(segments, colors=colors, linewidths=5) # possibly add zorder=...                      

最后,我们将所有内容都放在画布上

# plot the function and the line collection
fig, ax = plt.subplots()                                                         
ax.plot(x,y)                                                                     
ax.add_collection(lc) 

答案 1 :(得分:0)

我建议添加一个imshow()和正确的extent,例如:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colorbar as colorbar
import matplotlib.colors as colors

### generate some data
np.random.seed(19680801)
npts = 50
x = np.random.uniform(0, 1, npts)
y = np.random.uniform(0, 1, npts)
X,Y=np.meshgrid(x,y)
z = x * np.exp(-X**2 - Y**2)*100

### create a colormap of three distinct colors for each landmass
landmass_cmap=colors.ListedColormap(["b","r","g"])
x_land=np.linspace(0,1,len(x)) ## this should be scaled to your "location"

## generate some fake landmass types (either 0, 1, or 2) with probabilites
y_land=np.random.choice(3, len(x), p=[0.1, 0.6, 0.3]) 
print(y_land) 

fig=plt.figure()
ax=plt.axes()
clev=np.arange(0.,50.,.5)

## adjust the "height" of the landmass 
x0,x1=0,1
y0,y1=0,0.05 ## y1 is the "height" of the landmass
## make sure that you're passing sensible zorder here and in your .contourf()
im = ax.imshow(y_land.reshape((-1,len(x))),cmap=landmass_cmap,zorder=2,extent=(x0,x1,y0,y1))
plt.contourf(x,y,z,clev,extend='max',zorder=1)

ax.set_xlim(0,1)
ax.set_ylim(0,1)

ax.plot()
ax.set_xlabel('Location')
ax.set_ylabel('Height above ground level [m]')
cbar = plt.colorbar()
cbar.ax.set_ylabel('o3 mixing ratio [ppb]')

## add a colorbar for your listed colormap
cax = fig.add_axes([0.2, 0.95, 0.5, 0.02]) # x-position, y-position, x-width, y-height
bounds = [0,1,2,3]
norm = colors.BoundaryNorm(bounds, landmass_cmap.N)
cb2 = colorbar.ColorbarBase(cax, cmap=landmass_cmap,
                                norm=norm,
                                boundaries=bounds,
                                ticks=[0.5,1.5,2.5],
                                spacing='proportional',
                                orientation='horizontal')
cb2.ax.set_xticklabels(['sea','land','ice'])

plt.show()

产量:

Exemplary plot now with legend