- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 tkinter 中嵌入了两个不同的 matplotlib 图形,我使用按钮在它们之间进行切换。第一个图形切换周期按预期绘制图形,但此后每次在两个图形之间切换都会导致第一个图形具有意外的 x 和 y 轴标签集以及范围从 0 到 1 的刻度。所需的图形一个 x 和 y 轴标签和刻度按预期出现,但此外,额外的 0 到 1 范围标签/刻度覆盖在预期标签/刻度上。图表的两个 x 和 y 轴标签/刻度表现正常,并且没有意外的重叠。以下是我的代码的简化版本,后面是一些显示问题的图表图片。
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
import numpy as np
# Separated out config of plot to just do it once
def config_plot():
fig = plt.figure(figsize=(18, 5))
return (fig)
class graphSwitcher:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.fig = config_plot()
self.graphIndex = 0
self.canvas = FigureCanvasTkAgg(self.fig, self.master)
self.config_window()
self.graph_one(self.fig)
self.frame.pack(expand=YES, fill=BOTH)
def config_window(self):
self.canvas.mpl_connect("key_press_event", self.on_key_press)
toolbar = NavigationToolbar2Tk(self.canvas, self.master)
toolbar.update()
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH,
expand=1)
self.button = Button(self.master, text="Quit",
command=self._quit)
self.button.pack(side=BOTTOM)
self.button_switch = Button(self.master, text="Switch Graphs",
command=self.switch_graphs)
self.button_switch.pack(side=BOTTOM)
plt.subplots_adjust(bottom=0.2)
# the def creates the first matplotlib graph
def graph_one(self, fig):
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
self.ax = fig.subplots()
try:
self.ax1.clear() # clear current axes
self.ax2.clear()
except AttributeError:
pass
self.ax.plot(t, s)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='Graph One')
self.canvas.draw()
# This def creates the second matplotlib graph that uses subplot
# to place two graphs one on top of the other
def graph_two(self, fig):
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
self.ax1 = plt.subplot2grid((5, 4), (0, 0), rowspan=4,
colspan=4)
self.ax1.plot(x1, y1, 'o-')
self.ax1.set(title='Graph Two')
means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)
self.ax2 = plt.subplot2grid((5, 4), (4, 0), sharex=self.ax1,
rowspan=1, colspan=4)
self.ax2.bar(std_men, means_men, color='green', width=0.5,
align='center')
self.canvas.draw()
def on_key_press(event):
key_press_handler(event, canvas, toolbar)
def _quit(self):
self.master.quit() # stops mainloop
def switch_graphs(self):
self.graphIndex = (self.graphIndex + 1 ) % 2
if self.graphIndex == 0:
self.graph_one(self.fig)
else:
self.graph_two(self.fig)
def main():
root = Tk()
graphSwitcher(root)
root.mainloop()
if __name__ == '__main__':
main()
以下是按顺序看到的图表的图片,显示前两次看到各个图表(周期 1)时,图表具有正确的轴标签和刻度。但第二次显示第一个图时,出现了一组额外的意外 x 和 y 轴标签和刻度。最后两张图在每个周期重复(按两次按钮)
如果有人对如何消除图一上出现的意外 x 和 y 轴标签/刻度有任何想法,我将不胜感激。
最佳答案
您正在清除在调用graph_two
时创建的轴,但您并没有删除它们。因此,当您第二次绘制第一个图表时,graph_two
轴仍然位于 graph_one
轴下方。您看不到这两个子图本身,因为它们位于下面,但轴刻度标签确实显示在边缘,这就是您所看到的。
如果您在 graph_one
中删除绘图和图形创建,则更容易看到这一点,您将能够看到 2 个子图被清除,但按下按钮时并未删除。
因此解决方案很简单,使用axes.remove
反而。所以 graph_one
看起来像:
def graph_one(self, fig):
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
self.ax = fig.subplots()
try:
self.ax1.remove() # remove current axes
self.ax2.remove()
except AttributeError:
pass
self.ax.plot(t, s)
self.ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='Graph One')
self.canvas.draw()
关于python - 当在 tkinter 中嵌入的两个 matplotlib 图形之间切换时,第一个图形上会出现一组额外的意外 x 轴标签和刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54239757/
我无法在此图中定位轴标签。我喜欢放置顶部标签,使管道与网格对齐,并放置左右标签,以便它们不接触绘图。 我试过了 ax.tick_params(axis='both', which='both'
我使用的是 python 2,下面的代码只是使用了一些示例数据,我的实际数据可能有不同的长度,并且可能不是很细。 import numpy as np import datetime i
给定坐标 [1,5,7,3,5,10,3,6,8]为 matplotlib.pyplot ,如何突出显示或着色线条的不同部分。例如,列表中的坐标 1-3 ( [1,5,7,3] ) 表示属性 a .我
我正在matplotlib中绘制以下图像。 我的问题是,图像看起来像这样,但是,我想使背景变暗,因为当我打印该图像时,灰度部分不会出现在打印物中。有人可以告诉我API进行此更改吗? 我使用简单的API
这是关于matplotlib的一个非常基本的问题,但是我不知道该怎么做: 我想绘制多个图形,并使用绘制窗口中的箭头从一个移到另一个。 目前,我只知道如何创建多个图并将其绘制在不同的窗口中,如下所示:
在 matplotlib 中绘制小块对象时,由于显示分辨率而引入了伪影。使用抗锯齿并不能解决问题。 这个问题有解决方案吗? import matplotlib.pyplot as plt impo
对于直方图,有一个简单的内置选项 histtype='step' .如何制作相同风格的条形图? 最佳答案 [阅读评论后添加答案] 将可选关键字设置为 fill=False对于条形图: import m
我正在尝试在 (6X3) 网格上创建子图。我对图例的位置有疑问。图例对所有子图都是通用的。 lgend 现在与 y 轴标签重叠 我尝试删除 constrained_layout=True 选项。但这在
我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段
我想创建一个带有对齐不同曲线文本的图例的图。这是一个最小的工作示例: import matplotlib.pyplot as plt import numpy as np x=np.linspace(
可以说我正在用matplotlib绘制一条线并添加一个图例。 在图例中,其显示为------ Label。当绘制较小的图形尺寸以进行打印时,我发现该行的默认水平长度太长。 是否存在将------ La
我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点? 我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Pyt
我有一个我想使用的绘图布局,其中 9 个不同的数据簇被布置在一个方形网格上。网格中的每个框都包含 3 个并排布置的箱线图。 我最初的想法是这将适合 3x3 子图布局,每个单独的子图本身被划分为 3x1
我的图形从y=-1变为y=10 我想在任意位置写一小段文字,例如x=2000,y=5: ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
我想使用LateX格式来构建一个表达式,其中出现一些数字,但这些数字是用LateX表达式中的变量表示的。 实际的目标是在axes.annotate()方法中使用它,但是为了讨论起见,这里是一个原理代码
我需要比较两组的二维分布。 当我使用 matplotlib.pyplot.contourf并覆盖图,每个等高线图的背景颜色填充整个图空间。有没有办法让每个等高线图的最低等高线级别透明,以便更容易看到每
在R中,有一个locator函数,类似于Matlab的ginput,您可以用鼠标单击图形并选择任何x,y坐标。此外,还有一个名为identify(x,y)的函数,如果您给它绘制了一组绘制的点x,y,然
我想用matplotlib生成矢量图。我尽力了-但输出是光栅图像。这是我使用的: import matplotlib matplotlib.use('Agg') import matplotlib.p
我正在尝试使用 matplotlib 制作具有非常小的灰点的散点图。由于点密度的原因,点需要很小。问题是 scatter() 函数的标记似乎既有线条又有填充。当标记很小时,只有线条可见,而看不到填充,
我不太明白为什么我无法在指定的限制内创建水平和垂直线。我想用这个框绑定(bind)数据。然而,双方似乎并没有遵守我的指示。为什么是这样? # CREATING A BOUNDING BOX # BOT
我是一名优秀的程序员,十分优秀!