我有一个应用程序,其中嵌入了一个交互式绘图窗口。我想在一个共享的x轴和一个独立的y轴上放置几个子图。整个过程都是为了进行数据分析。
我已经建立了子图,但是由于某种原因,第一个子图的y轴以某种方式连接到其他子图,但并非以其他方式连接。 此外,刻度线和标签重叠,而不是停留在其各自的子图上。
我尝试使用pyplot.subplots
函数来解决独立轴和刻度/标签的问题,但是当我调用它而不是嵌入它时,这会打开另一个窗口。
展示问题的示例代码(Python 3.7)
# -*- coding: utf-8 -*-
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from tkinter import ttk
import numpy as np
import pandas as pd
class window(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
self.generate_data()
self.plotPane = self.plot_pane(self)
def generate_data(self):
self.data = pd.DataFrame()
x = [1,2,3,4,5]
y = np.zeros((len(x)))
for i,yy in enumerate(y):
y[i] = yy + 10.*float(np.random.random())
self.data['x'] = x
self.data['y'] = y
class plot_pane():
def __init__(self,parent):
self.parent = parent
self.labelframe = ttk.Labelframe(self.parent,text="Plot")
replotButton = ttk.Button(self.parent,text="replot",command=self.update_plot)
self.labelframe.grid(row=0, column=0)
replotButton.grid(row=1,column=0)
self.figure = Figure(figsize=(6,3),dpi=100)
self.figure.subplots_adjust(left=0.11,bottom=0.09,right=0.77,top=0.92)# BaMa:. subplot margins, so labels are visible
self.sub = self.figure.add_subplot(111)
# toolbar and canvas for live plot
canvas = FigureCanvasTkAgg(self.figure,self.labelframe)
canvas.get_tk_widget().grid(row=1,column=1,rowspan=2,sticky="NSEW",pady=20)
canvas._tkcanvas.grid(row=1,column=1,rowspan=2, sticky="NSEW")
toolbar_frame = tk.Frame(self.labelframe)
toolbar_frame.grid(row=0,column=1,sticky="SEW")
toolbar = NavigationToolbar2Tk(canvas,toolbar_frame)
toolbar.update()
self.update_plot()
def update_plot(self,*args):
# clear figure
self.figure.clf()
stackAx = []
for i in range(0,2):
# generate new random data
self.parent.generate_data()
testX = self.parent.data['x']
testY = self.parent.data['y']
# add subplots
if i == 0:
stackAx.append(self.figure.add_subplot(i+1,1,1))# y-axis of this subplot is somehow connected to the other
else:
stackAx.append(self.figure.add_subplot(i+1,1,1,sharex=stackAx[0]))
# plot, add labels, ticks and grid
stackAx[i].plot(testX,testY)
stackAx[i].set_ylabel("ax-"+str(i))
stackAx[i].tick_params('y')
stackAx[i].grid()
self.figure.canvas.draw()
window = window()
window.mainloop()
因此,当您在顶部移动子图时,底部y轴也会移动,并且“ ax-0”标签+刻度线会突破上方的子图。当您移动下部图时,上部图的y轴不会不移动(应该如此)
答案 0 :(得分:0)
我知道了。显然add_subplot
和pyplot.subplots
的功能有些不同,我无法正确理解。
以下更新功能有效:
def update_plot(self,*args):
# clear figure
self.figure.clf()
stackAx = []
numberOfPlots = 2
for i in range(0,numberOfPlots):
# generate new random data
self.parent.generate_data()
testX = self.parent.data['x']
testY = self.parent.data['y']
# add subplots
if i == 0:
stackAx.append(self.figure.add_subplot(numberOfPlots,1,i+1))
else:
stackAx.append(self.figure.add_subplot(numberOfPlots,1,i+1,sharex=stackAx[0]))
# plot, add labels, ticks and grid
stackAx[i].plot(testX,testY)
stackAx[i].set_ylabel("ax-"+str(i))
stackAx[i].tick_params('y')
stackAx[i].grid()
self.figure.canvas.draw()
我从这里得到的:https://pythonprogramming.net/subplot2grid-add_subplot-matplotlib-tutorial/