(更新进度)使用 Matplotlib 单选按钮在等高线图之间切换 [Python]

(Updated Progress) Switching between contour plots with Matplotlib Radio Button [Python]

大家晚上好,

我尝试编写一个程序,使用 matplotlib radio buttons 在同一轴上交替显示两个等高线图。

两个等高线图在每个给定自己的图框时都成功绘制。等高线图(由字典 density_Adensity_Z 表示,它们是彼此的镜像。请参见下图

更新

是的,所以我实际上能够在单击 radio 按钮时用密度 Z 图替换密度 A 图。这是对通常会取代密度 A 的灰色斑点的改进。请参见下图。

未解决的问题:

  1. 单击 radio button Density A 时,绘图不会恢复为原始密度 A 图。

我在 def change_plot 函数中尝试了一系列 if/else statements

欢迎提出任何建议。

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
from matplotlib.widgets import RadioButtons


  # Junk data for the purpose of my question

x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
density_A = [1, 1, 1, 1, 0, 1, 1, 1, 1]
density_Z = [0, 0, 0, 0, 1, 0, 0, 0, 0]

fig, (ax1, ax2) = plt.subplots(1, 2)

"""
# -----------------------
# Interpolation on a grid
# -----------------------
# A contour plot of irregularly spaced data coordinates
# via interpolation on a grid.
"""
ngridx = 1000
ngridy = 2000

# Create grid values first.
xi = np.linspace(-2.1, 2.1, ngridx)
yi = np.linspace(-2.1, 2.1, ngridy)

# Linearly interpolate the data (x, y) on a grid defined by (xi, yi).
triang = tri.Triangulation(x, y)
interpolator = tri.LinearTriInterpolator(triang, density_A)
Xi, Yi = np.meshgrid(xi, yi)
zi = interpolator(Xi, Yi)

"""
# ----------
# Tricontour
# ----------

Contour plot is generated here.

"""

ax1.tricontour(x, y, density_A, levels=100, linewidths=0.25, colors='k')
cntr2 = ax1.tricontourf(x, y, density_A, levels=100, cmap="terrain")  # Colour Bar has colour scheme RdBu_r

ax2.tricontour(x, y, density_Z, levels=100, linewidths=0.25, colors='k')
cntr3 = ax2.tricontourf(x, y, density_Z, levels=100, cmap="terrain")  # Colour Bar has colour scheme RdBu_r

"""
# ----------
# Plot Setup
# ----------

Defining the layout for ax1, ax2 and radio box
"""


fig.colorbar(cntr2, ax=ax1)
ax1.set_title('Density A')
ax1.plot(x, y, 'ko', ms=2.5)
ax1.set(xlim=1, ylim=1)  # Sets up the grid

fig.colorbar(cntr3, ax=ax2)
ax2.set_title('Density Z')
ax2.plot(x, y, 'ko', ms=2.5)
ax2.set(xlim=1, ylim=1)  # Sets up the grid

  # Radio box formmating


axcolor = 'lightgoldenrodyellow'
rax = plt.axes([.05, 0.7, 0.15, 0.15])
radio = RadioButtons(rax, ('Density A', 'Density Z'))

def change_plot(label):
    density_options = {'Density A': ax1.tricontourf(x, y, density_A, levels=100, cmap="terrain") and ax1.set_title('Density A') and ax1.plot(x, y, 'ko', ms=2.5),
                       'Density Z': ax1.tricontourf(x, y, density_Z, levels=100, cmap="terrain") and ax1.set_title('Density Z') and ax1.plot(x, y, 'ko', ms=2.5)}
    plt.draw()


radio.on_clicked(change_plot)  #change plot to the corresponding radio button

plt.show()  #display the plot

好的,我解决了。下图!

我的解决方案需要 change_plot 函数中的 if/else 语句。



def change_plot(label):
    if label == 'Density A':
        ax1.cla()
        ax1.tricontourf(x, y, density_A, levels=100, cmap="terrain")
        ax1.set_title('Density A')
        ax1.plot(x, y, 'ko', ms=2.5)
        plt.draw()

    else:
        ax1.cla()
        ax1.tricontourf(x, y, density_Z, levels=100, cmap="terrain")
        ax1.set_title('Density Z')
        ax1.plot(x, y, 'ko', ms=2.5)
        plt.draw()