没有像素决定的标记位置?
Marker position without pixel decision?
如果我设置小 matplotlib 标记的位置,它们必须“决定”选择哪个像素。我认为在 Matlab 中情况并非如此,标记可以位于像素之间,两个像素共享亮度。
在附加的示例中,我屏幕上的标记没有执行平滑的圆,而是在像素之间跳跃。在 Matlab 中,这个圆圈看起来很平滑。我可以使用 matplotlib 实现同样的效果吗?
import tkinter as tk
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import time
class Plotwindow():
def __init__(self, root, size):
self.root = root
self.fig = Figure(size, constrained_layout=True)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.fig, master=root.plot_frame)
self.canvas.get_tk_widget().pack()
def clear(self):
self.ax.cla()
def plot(self, x, y):
self.ax.scatter(x, y, marker='o', s=2, color='black', snap=False)
self.ax.set_xlim(20*np.array([-1, 1]))
self.ax.set_ylim(20*np.array([-1, 1]))
self.canvas.draw()
self.canvas.flush_events()
class NewGUI():
def __init__(self):
self.root = tk.Tk()
self.root.resizable(False, False)
self.root.geometry("405x405+400+200")
self.button = tk.Button(self.root, text="Move", command=self.move)
self.button.pack(side='top', fill='x')
self.button = tk.Button(self.root, text="Hold", command=self.hold)
self.button.pack(side='top', fill='x')
self.plot_frame = tk.Frame()
self.plot_frame.pack(side='bottom', fill='x')
self.plot_window = Plotwindow(self, (15,15))
self.plot_window.plot(1,0)
self.root.mainloop()
def move(self):
for i in range(20):
self.plot_window.clear()
time.sleep(0.01)
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
def hold(self):
for i in range(20):
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
if __name__ == '__main__':
new = NewGUI()
根据 Jody Klymak 的评论,即使没有 snap=False,我也通过 cairo 后端得到了我想要的东西,请参阅附件。
import tkinter as tk
import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkCairo')
from matplotlib.backends.backend_tkcairo import FigureCanvasTkCairo
from matplotlib.figure import Figure
import time
class Plotwindow():
def __init__(self, root, size):
self.root = root
self.fig = Figure(size, constrained_layout=True)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkCairo(self.fig, master=root.plot_frame)
self.canvas.get_tk_widget().pack()
def clear(self):
self.ax.cla()
def plot(self, x, y):
self.ax.scatter(x, y, marker='o', s=2, color='black')
self.ax.set_xlim(20*np.array([-1, 1]))
self.ax.set_ylim(20*np.array([-1, 1]))
self.canvas.draw()
self.canvas.flush_events()
class NewGUI():
def __init__(self):
self.root = tk.Tk()
self.root.resizable(False, False)
self.root.geometry("405x405+400+200")
self.button = tk.Button(self.root, text="Move", command=self.move)
self.button.pack(side='top', fill='x')
self.button = tk.Button(self.root, text="Hold", command=self.hold)
self.button.pack(side='top', fill='x')
self.plot_frame = tk.Frame()
self.plot_frame.pack(side='bottom', fill='x')
self.plot_window = Plotwindow(self, (15,15))
self.plot_window.plot(1,0)
self.root.mainloop()
def move(self):
for i in range(20):
self.plot_window.clear()
time.sleep(0.01)
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
def hold(self):
for i in range(20):
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
if __name__ == '__main__':
new = NewGUI()
如果我设置小 matplotlib 标记的位置,它们必须“决定”选择哪个像素。我认为在 Matlab 中情况并非如此,标记可以位于像素之间,两个像素共享亮度。
在附加的示例中,我屏幕上的标记没有执行平滑的圆,而是在像素之间跳跃。在 Matlab 中,这个圆圈看起来很平滑。我可以使用 matplotlib 实现同样的效果吗?
import tkinter as tk
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import time
class Plotwindow():
def __init__(self, root, size):
self.root = root
self.fig = Figure(size, constrained_layout=True)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.fig, master=root.plot_frame)
self.canvas.get_tk_widget().pack()
def clear(self):
self.ax.cla()
def plot(self, x, y):
self.ax.scatter(x, y, marker='o', s=2, color='black', snap=False)
self.ax.set_xlim(20*np.array([-1, 1]))
self.ax.set_ylim(20*np.array([-1, 1]))
self.canvas.draw()
self.canvas.flush_events()
class NewGUI():
def __init__(self):
self.root = tk.Tk()
self.root.resizable(False, False)
self.root.geometry("405x405+400+200")
self.button = tk.Button(self.root, text="Move", command=self.move)
self.button.pack(side='top', fill='x')
self.button = tk.Button(self.root, text="Hold", command=self.hold)
self.button.pack(side='top', fill='x')
self.plot_frame = tk.Frame()
self.plot_frame.pack(side='bottom', fill='x')
self.plot_window = Plotwindow(self, (15,15))
self.plot_window.plot(1,0)
self.root.mainloop()
def move(self):
for i in range(20):
self.plot_window.clear()
time.sleep(0.01)
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
def hold(self):
for i in range(20):
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
if __name__ == '__main__':
new = NewGUI()
根据 Jody Klymak 的评论,即使没有 snap=False,我也通过 cairo 后端得到了我想要的东西,请参阅附件。
import tkinter as tk
import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkCairo')
from matplotlib.backends.backend_tkcairo import FigureCanvasTkCairo
from matplotlib.figure import Figure
import time
class Plotwindow():
def __init__(self, root, size):
self.root = root
self.fig = Figure(size, constrained_layout=True)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkCairo(self.fig, master=root.plot_frame)
self.canvas.get_tk_widget().pack()
def clear(self):
self.ax.cla()
def plot(self, x, y):
self.ax.scatter(x, y, marker='o', s=2, color='black')
self.ax.set_xlim(20*np.array([-1, 1]))
self.ax.set_ylim(20*np.array([-1, 1]))
self.canvas.draw()
self.canvas.flush_events()
class NewGUI():
def __init__(self):
self.root = tk.Tk()
self.root.resizable(False, False)
self.root.geometry("405x405+400+200")
self.button = tk.Button(self.root, text="Move", command=self.move)
self.button.pack(side='top', fill='x')
self.button = tk.Button(self.root, text="Hold", command=self.hold)
self.button.pack(side='top', fill='x')
self.plot_frame = tk.Frame()
self.plot_frame.pack(side='bottom', fill='x')
self.plot_window = Plotwindow(self, (15,15))
self.plot_window.plot(1,0)
self.root.mainloop()
def move(self):
for i in range(20):
self.plot_window.clear()
time.sleep(0.01)
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
def hold(self):
for i in range(20):
phi = 2*np.pi*i/20
x = np.cos(phi)
y = np.sin(phi)
self.plot_window.plot(x,y)
if __name__ == '__main__':
new = NewGUI()