简体   繁体   中英

Matplotlib Lines is plotting extra lines in my plot

I'm trying to visualize a pair of two lists, represented by lines_x and lines_y which are meant to be plugged into the coordinates argument of either the plot function in Axes or in Lines2D.

Right now, I'm getting this result, which has extra lines compared to the result I am trying to get. What I'm currently getting:

在此处输入图片说明

Previously, I tried using a loop to plot the lines one by one, and that worked for a while. However, after a few runs, it no longer worked.

Could someone please suggest a way for me to achieve the following result on my window?

The plot I want to achieve:

在此处输入图片说明

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

It makes sense if you annotate the order in which the line segments are drawn. For example (only plotting the first 10 points, otherwise it becomes a bit of a mess):

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)

Results in:

在此处输入图片说明

If I increase n , it becomes a larger mess since a number of x,y coordinates are duplicates. So:

  1. Make sure that there are no duplicate coordinates
  2. Make sure that the coordinates are ordered correctly.

Try these sequences instead:

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]

Worked for me:

在此处输入图片说明

Also I should note that I used simply

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot(lines_x,lines_y)

So I believe Lines has nothing to do with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM