简体   繁体   English

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

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

Good Evening All,大家晚上好,

I've attempted to write a programme that alternates between two contour plots on the same axis with the use of matplotlib radio buttons .我试图编写一个程序,该程序使用matplotlib radio buttons在同一轴上的两个等高线图之间交替。

Both contour plots successfully plot when each given their own plot box.当每个等高线图都有自己的图框时,两个等高线图都能成功绘制。 The contour plots (represented by dictionaries density_A and density_Z are mirror images of each other. See image below等高线图(由字典density_Adensity_Z是彼此的镜像。见下图

在此处输入图片说明

Update更新

Right, so I'm actually able to get the Density Z plot replace Density A plot when clicking on the radio button.是的,所以我实际上能够在单击radio按钮时将密度 Z 图替换密度 A 图。 Which is an improvement over the grey blob that would normally replace Density A. See image below.这是对通常替换密度 A 的灰色斑点的改进。见下图。

在此处输入图片说明

Unresolved Issues:尚未解决的问题:

  1. When clicking on radio button Density A the plot does not revert to original Density A plot.单击radio button Density A ,绘图不会恢复为原始密度 A 绘图。

I've attempted a series of if/else statements within my def change_plot function.我在我的def change_plot函数中尝试了一系列if/else statements

Any suggestions would be welcomed.任何建议将受到欢迎。

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

Okay, I solved it.好的,我解决了。 Image below!下图!

My solution requires an if/else statement within the change_plot function.我的解决方案需要在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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM