我正在尝试可视化一对由lines_x
和lines_y
表示的两个列表,这两个列表用于插入Axes中plot
函数的坐标参数或在Lines2D。
现在,我得到了这个结果,与我想要获得的结果相比,它有额外的行。 我目前得到的是什么:
以前,我尝试使用循环逐行绘制线条,并且工作了一段时间。但是,经过几次运行后,它就不再有用了。
有人可以建议我在我的窗口上获得以下结果吗?
我想要实现的情节:
from pylab import *
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pylab as plt
import matplotlib.pyplot as pltplot
import matplotlib.lines
from matplotlib.collections import LineCollection
matplotlib.use ("gTkAgg")
import numpy as np
import tkinter as tk
from tkinter import Tk
from tkinter import *
class Window (Frame):
lines_x = [-2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 0, 1, 1, 2, -2, 2, -2, -1, -1, 0]
lines_y = [0, 1, 1, 2, -2, 2, -2, -1, -1, 0, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2]
def __init__(self, parent = None):
Frame.__init__(self,parent)
parent.title("Shape Grammar Interpreter")
self.top=Frame()
self.top.grid()
self.top.update_idletasks
self.menu()
self.makeWidgets()
def makeWidgets(self):
self.f = Figure(figsize = (6,6), dpi = 100)
self.a = self.f.add_subplot(111)
#self.a.plot(self.lines_x, self.lines_y, linewidth = 4.0, picker=5)
line = Line2D(self.lines_x, self.lines_y)
self.a.add_line(line)
for i in range(len(self.lines_x)):
self.a.plot(self.lines_x[i:i+1], self.lines_y[i:i+1], linewidth = 4.0)
#self.a.plot(lines_x, lines_y, linewidth = 4.0, color = "blue")
self.a.margins(y=0.5)
self.a.margins(x=0.5)
#self.a.axes.get_xaxis().set_visible(False)
#self.a.axes.get_yaxis().set_visible(False)
# a tk.DrawingArea
self.canvas = FigureCanvasTkAgg(self.f, master=self.top)
#to show window
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def menu(self):
menubar = Menu (root)
#to close window
menubar.add_command(label="Exit", command=self.quit_window)
root.config(menu=menubar)
def quit_window(self):
root.quit()
root.destroy()
if __name__ == "__main__":
root = Tk()
my_gui = Window(root)
root.mainloop()
答案 0 :(得分:1)
如果您注释绘制线段的顺序,则有意义。例如(仅绘制前10个点,否则会变得有点混乱):
import matplotlib.pylab as pl
lines_x = [-2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 0, 1, 1, 2, -2, 2, -2, -1, -1, 0]
lines_y = [0, 1, 1, 2, -2, 2, -2, -1, -1, 0, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2]
n = 10
pl.figure()
pl.plot(lines_x[:n], lines_y[:n])
# Number the coordinates to indicate their order:
for i in range(len(lines_x[:n])):
pl.text(lines_x[i], lines_y[i], '{}'.format(i))
pl.xlim(-3,3)
pl.ylim(-3,3)
结果:
如果我增加n
,它会变得更大,因为许多x,y
坐标都是重复的。所以:
答案 1 :(得分:0)