簡體   English   中英

在matplotlib.pyplot圖中使用傳遞的軸對象?

[英]Using passed axis objects in a matplotlib.pyplot figure?

我目前正在嘗試使用在函數中創建的傳遞軸對象,例如:

def drawfig_1():
    import matplotlib.pyplot as plt

    # Create a figure with one axis (ax1)
    fig, ax1 = plt.subplots(figsize=(4,2))

    # Plot some data
    ax1.plot(range(10))

    # Return axis object
    return ax1

我的問題是,如何在另一個圖中使用返回的軸對象ax1? 例如,我想以這種方式使用它:

# Setup plots for analysis
fig2 = plt.figure(figsize=(12, 8))

# Set up 2 axes, one for a pixel map, the other for an image
ax_map = plt.subplot2grid((3, 3), (0, 0), rowspan=3)
ax_image = plt.subplot2grid((3, 3), (0, 1), colspan=2, rowspan=3)

# Plot the image
ax_psf.imshow(image, vmin=0.00000001, vmax=0.000001, cmap=cm.gray)

# Plot the map
????      <----- #I don't know how to display my passed axis here...

我嘗試過如下聲明:

ax_map.axes = ax1

雖然我的腳本沒有崩潰,但我的軸空了。 任何幫助,將不勝感激!

您正在嘗試先繪制一個繪圖,然后將該繪圖作為另一個繪圖中的子繪圖(由subplot2grid定義)。 不幸的是,這是不可能的。 另見這篇文章: 如何將matplotlib圖對象包含為子圖?

您必須首先制作子圖並將子圖的軸傳遞給drawfig_1()函數以繪制它。 當然,需要修改drawfig_1() 例如:

def drawfig_1(ax1):
    ax1.plot(range(10))
    return ax1

# Setup plots for analysis
fig2 = plt.figure(figsize=(12, 8))

# Set up 2 axes, one for a pixel map, the other for an image
ax_map = plt.subplot2grid((3, 3), (0, 0), rowspan=3)
ax_image = plt.subplot2grid((3, 3), (0, 1), colspan=2, rowspan=3)

# Plot the image
ax_image.imshow(image, vmin=0.00000001, vmax=0.000001, cmap=cm.gray)
# Plot the map:
drawfig_1(ax_map)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM