【问题标题】:Matplotlib Lines is plotting extra lines in my plotMatplotlib Lines 在我的情节中绘制额外的线条
【发布时间】:2016-11-17 09:02:59
【问题描述】:

我正在尝试可视化由lines_xlines_y 表示的两个列表对,它们旨在插入到Axes 或Lines2D 中plot 函数的坐标参数中。

现在,我得到了这个结果,与我试图得到的结果相比,它有额外的行。 我目前得到的:

以前,我尝试使用循环来逐条绘制线条,并且效果好一段时间。但是,运行几次后,它不再起作用了。

有人可以建议我在我的窗口上实现以下结果的方法吗?

我想要达到的情节:

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()

【问题讨论】:

    标签: python python-3.x matplotlib plot tkinter


    【解决方案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. 确保没有重复的坐标
    2. 确保坐标排序正确。

    【讨论】:

    • 不是我想要的,但谢谢。已经解决了这个问题。
    【解决方案2】:

    请尝试以下顺序:

    lines_x = [-2, -2, -1, -1,  0,  0,  1,  1,  2, 2, -2]
    lines_y = [ 0,  1,  1,  2,  2, -2, -2, -1, -1, 0,  0]
    

    为我工作:

    另外我应该注意我只是简单地使用了

    In [1]: import matplotlib.pyplot as plt
    
    In [2]: plt.plot(lines_x,lines_y)
    

    所以我认为 Lines 与它无关。

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多