为具有不同数据范围的绘图创建单个颜色条

时间:2021-02-15 18:30:41

标签: python matplotlib tkinter

我正在尝试创建一个包含三个子图的图表。每个子图都有不同范围的数据,但我想在我的图右侧创建一个颜色条,可用于所有三个子图。缩放应该使得所有数据的最大值对应于颜色条的最大值,并且所有数据的最小值对应于颜色条的最小值。然后应相应地缩放三个子图。这是我的示例代码。

from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import cm
import numpy as np

from tkinter.filedialog import askopenfilename
import tkinter as tk

from math import sin, cos, pi

polar = False
filename = ''

def File_Select():
    global filename
    
    filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file

def Plot():
        m = 501
        n = 101
        
        X = np.zeros((m,n))
        Y = np.zeros((m,n))
        DATA_1 = np.zeros((m,n))
        DATA_2 = np.zeros((m,n))
        DATA_3 = np.zeros((m,n))
        
        for i in range(m):
            for j in range(n):
                X[i][j] = i
                Y[i][j] = j
                DATA_1[i][j] = 5*sin(i*pi/(m-1))
                DATA_2[i][j] = 0.1*cos(1.5*j*pi/(n-1))
                DATA_3[i][j] = 2*sin(5*j*pi/(m-1))+cos(5*i*pi/(n-1))
    
        # Define subplot shapes
        fig = Figure(figsize = (15, 8), dpi = 100, tight_layout=True)
        
        if(m<=n):
            plot1 = fig.add_subplot(131) 
            plot2 = fig.add_subplot(132)  
            plot3 = fig.add_subplot(133) 
        else:
            plot1 = fig.add_subplot(311) 
            plot2 = fig.add_subplot(312)
            plot3 = fig.add_subplot(313)
     
        # Plot contour plots
        plot1.contourf(X, Y, DATA_1, cmap=cm.jet)
        plot1.set_aspect('equal', 'box')
        
        plot2.contourf(X, Y, DATA_2, cmap=cm.jet)
        plot2.set_aspect('equal', 'box')
        
        plot3.contourf(X, Y, DATA_3, cmap=cm.jet)
        plot3.set_aspect('equal', 'box')
        
        graph = FigureCanvasTkAgg(fig, master = root2)   
        graph.draw() 
        graph.get_tk_widget().grid(row=1,column=0,columnspan=2)
        
        root2.update()

root1 = tk.Tk()
root2 = tk.Tk()

root1.title('Recreate Plots')
root2.title('Contour Plots')

Plot_Button = tk.Button (root1, text='Plot', command=Plot, bg='Green', font=('Arial', 20, 'bold'))
Plot_Button.grid(row=1,column=0,padx=100,pady=10)

Exit_Button = tk.Button (root1, text='Exit Application', command=root1.quit, bg='orange', font=('Arial', 20, 'bold'))
Exit_Button.grid(row=2,column=0,padx=100,pady=10)

root1.mainloop()

root1.destroy()
root2.destroy()

m<=n if-else 语句用于根据需要从上到下或从边到边堆叠图。

if(m<=n):
    plot1 = fig.add_subplot(131) 
    plot2 = fig.add_subplot(132)  
    plot3 = fig.add_subplot(133) 
else:
    plot1 = fig.add_subplot(311) 
    plot2 = fig.add_subplot(312)
    plot3 = fig.add_subplot(313)

enter image description here

1 个答案:

答案 0 :(得分:0)

类似于:


fig, axs = plt.subplots(1, 3, constrained_layout=True)
dmin = np.inf
dmax = -np.inf
for data in (data1, data2, data3):
    dmin = np.min(np.min(data[:]))
    dmax = np.max(np.max(data[:]))
levels = np.linspace(dmin, dmax, 20)
for ax, data in zip(axs, (data1, data2, data3)):
    pc = ax.contourf(data, levels=levels)
fig.colorbar(pc, ax=axs)