Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用条件着色添加水平线_Python_Matplotlib - Fatal编程技术网

Python 使用条件着色添加水平线

Python 使用条件着色添加水平线,python,matplotlib,Python,Matplotlib,我使用matplotlib.pyplot制作了一个contourf绘图。现在我想要一条水平线(或者类似于ax.vspan的东西也可以),在y=0处使用条件着色。我会告诉你我有什么,我想得到什么。我想用一个数组,比如说landsurface,表示陆地、海洋或冰。此数组填充有1(陆地)、2(海洋)或3(冰),并具有len(locs)(因此x轴) # 1 when near min, 2 when near 0, 3 when near max z = np.where(y<-0.5, 1, n

我使用
matplotlib.pyplot
制作了一个
contourf
绘图。现在我想要一条水平线(或者类似于
ax.vspan
的东西也可以),在
y=0
处使用条件着色。我会告诉你我有什么,我想得到什么。我想用一个数组,比如说
landsurface
,表示陆地、海洋或冰。此数组填充有
1
(陆地)、
2
(海洋)或
3
(冰),并具有
len(locs)
(因此x轴)

# 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]                                                   
这是绘图代码:

# 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]                                                   
plt.figure()
ax=plt.axs()
clev=np.arange(0,50,5)
plt.contourf(locs、高度、表面高度、var、clev、extend='max')
plt.xlabel('位置')
plt.ylabel('地平面以上高度[m]”)
cbar=plt.colorbar()
cbar.ax.set_ylabel('o3混合比[ppb]”)
plt.show()
这就是我到目前为止所做的:

# 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]                                                   

# 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]                                                   
这就是我想要的:

# 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]                                                   

# 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]                                                   

非常感谢

我建议添加一个带有适当
范围的
imshow()
,例如:

# 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]                                                   
将numpy导入为np
将matplotlib.pyplot作为plt导入
将matplotlib.colorbar导入为colorbar
将matplotlib.colors导入为颜色
###生成一些数据
np.随机种子(196801)
净现值=50
x=np.随机.均匀(0,1,npts)
y=np.随机.均匀(0,1,npts)
十、 Y=np.meshgrid(X,Y)
z=x*np.exp(-x**2-Y**2)*100
###为每个地块创建三种不同颜色的彩色地图
landmass_cmap=colors.ListedColormap([“b”、“r”、“g”]))
x_land=np.linspace(0,1,len(x))##这应该缩放到您的“位置”
##生成一些带有概率的假地块类型(0、1或2)
y_land=np.随机选择(3,len(x),p=[0.1,0.6,0.3])
打印(y_land)
图=plt.图()
ax=plt.axs()
clev=np.arange(0,50,5)
##调整陆块的“高度”
x0,x1=0,1
y0,y1=0,0.05##y1是陆地的“高度”
##确保您在这里和您的.tourtf()中通过了明智的zorder
im=ax.imshow(y_land.reformate(-1,len(x)),cmap=landmass_cmap,zorder=2,extent=(x0,x1,y0,y1))
轮廓曲线图(x,y,z,clev,extend='max',zorder=1)
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.plot()
ax.set_xlabel('位置')
ax.set_ylabel('地平面以上的高度[m]”)
cbar=plt.colorbar()
cbar.ax.set_ylabel('o3混合比[ppb]”)
##为列出的颜色贴图添加颜色栏
cax=图添加_轴([0.2,0.95,0.5,0.02])#x位置,y位置,x宽度,y高度
界限=[0,1,2,3]
范数=颜色。边界范数(边界,陆块\u cmap.N)
cb2=色条、色条(cax,cmap=landmass\u cmap,
norm=norm,
边界=边界,
滴答声=[0.5,1.5,2.5],
间距‘’,
方向(水平)
cb2.ax.set_xticklabels([‘海’、‘陆’、‘冰’]))
plt.show()
收益率:

# 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]                                                   

简介
# 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]                                                   
我要用一个新的

# 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]                                                   
因为我没有您的原始数据,所以我使用简单的正弦曲线伪造了一些数据,并在基线上绘制了对应于曲线的小、中、高值的颜色代码

# 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]                                                   
代码 通常,我们需要显式导入
LineCollection

import matplotlib.pyplot as plt                                                  
import numpy as np                                                               
from matplotlib.collections import LineCollection
# 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]                                                   
只是画一条正弦曲线(xr 从曲线值(对应于曲面类型)到
LineCollection
颜色的颜色编码,请注意
LineCollection
要求将颜色指定为RGBA元组,但我看到过使用颜色字符串的示例,呸

# 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]                                                   
最后,我们把一切都放在画布上

# 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]                                                   
# plot the function and the line collection
fig, ax = plt.subplots()                                                         
ax.plot(x,y)                                                                     
ax.add_collection(lc) 

请看,这很有魅力,谢谢。有没有办法删除垂直的灰色/白色线条,使线条集合看起来连续?对于垂直线条,请尝试使用
抗锯齿=1
(或者是
抗锯齿=0
?)-否则,您可以尝试在每个段末端的x坐标上添加一个非常小的ε。免责声明,我没有测试这两个想法。@Bollehenk我检查过,它是
抗锯齿=0
…我编辑了答案,我还使用
zorder=3
强制在前景中绘制颜色编码线。