matplotlib多轴绘图顺序,图像在第二轴上

时间:2018-07-04 15:05:33

标签: python matplotlib imshow

我正在尝试使第二个y轴在第一个y轴上绘制。

如果将图像附加到第一轴,则可以执行此操作。 但是,我需要左轴进行对数缩放。通过对数缩放,图像也可以对数缩放。

因此,我将图像附加到第二(线性)轴上。但是然后我看不到第一个轴。因此,我将第一个轴设置为具有较高的zorder和透明色块。

但是,第一个轴位于第二个轴的顶部。 Z顺序的组合似乎没有使红线上方的绿线。

请参阅所附图片,了解当前代码的产生情况。

matplotlib image

import matplotlib.pyplot as plt                                                                                                                             
import random                                                                                                                                               
import numpy as np                                                                                                                                          

fig, ax = plt.subplots()                                                                                                                                    

# left log axis - left axis must be log                                                                                                                     
ax.set_ylabel('log')                                                                                                                                        
ax.set_ylim(1,1E6)                                                                                                                                          
ax.set_yscale("log")                                                                                                                                        
x = range(100)                                                                                                                                              
y = [random.randint(0,1E6) for i in x]                                                                                                                      
ax.scatter(x, y, marker='s', color='r')                                                                                                                     

# make this plot show above 2nd axis with zorder                                                                                                            
ax.set_zorder(50)                                                                                                                                           
ax.patch.set_visible(False) # must do this to see the 2nd axis at all                                                                                       

# generate an image                                                                                                                                         
def create_image():                                                                                                                                         
    delta = 0.025                                                                                                                                               x = y = np.arange(-3.0, 3.0, delta)                                                                                                                         X, Y = np.meshgrid(x, y)                                                                                                                                
    Z1 = np.exp(-X**2 - Y**2)                                                                                                                               
    Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)                                                                                                                   
    Z = (Z1 - Z2) * 2                                                                                                                                       
    return Z                                                                                                                                                

# right linear axis                                                                                                                                         
ax2 = ax.twinx()                                                                                                                                            

# image should be in the background. Can't put image on first axis, as the log axis makes the image log too!                                                
ax2.imshow(create_image(), zorder=0)                                                                                                                        
ax2.plot(x, x, color='g', zorder=100) # this plot should show above the first plot, but doesn't                                                             

plt.show()                    

0 个答案:

没有答案