[Matplotlib-users] Why are all my plots displayed as closed loops?

Christoph Gohlke cgohlke at uci.edu
Sun Jun 5 04:31:33 EDT 2016


On 6/4/2016 11:56 PM, Paul D. DeRocco wrote:
>> From: Thomas Caswell [mailto:tcaswell at gmail.com]
>>
>> `Line2D` draws lines connecting the points in the order you
>> pass them in.  If mpl sorted your data based on the x values
>> then you could never plot anything was not a single valued
>> function f(x).
>
> That's fine, but in this case that's exactly what I'm trying to plot, so
> I'm giving it ascending x values. But it's connecting the last point back
> to the first point, for no obvious reason. I won't try to post the screen
> caps in the mailing list, which I expect doesn't work, but they are at
> still at http://preview.tinyurl.com/hb62una for now. But here's the code:
>
> ---
> import numpy
> import gtk
> import matplotlib
> from matplotlib.figure import Figure
> from matplotlib.backends.backend_gtk import FigureCanvasGTK as
> FigureCanvas
>
> class junk:
>
>     def __init__(self):
>
>         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
>         self.window.connect('destroy', self.destroy)
>         self.window.set_title('junk')
>         self.window.resize(400, 400)
>
>         self.figure = Figure()
>         self.axes = self.figure.add_axes((0, 0, 1, 1))
>         self.canvas = FigureCanvas(self.figure)
>         self.canvas.show()
>
>         self.window.add(self.canvas)
>
>         self.axes.set_xlim(-10, 12)
>         self.axes.set_ylim(-1, 122)
>         x = numpy.arange(-9, 12)
>         self.axes.plot(x, x * x)
>
>         self.canvas.draw()
>
>         self.window.show_all()
>
>     def destroy(self, widget, data = None):
>
>         gtk.main_quit()
>
>     def main(self):
>
>         gtk.main()
>
> if __name__ == '__main__':
>
>     app = junk()
>     app.main()
> ---
>
> Do you get the same results? How do I make it not do that?
>

Looks like a bug in the GTK backend at least on Windows. Here's a 
shorter example

import numpy
import matplotlib
matplotlib.use('GTK')
from matplotlib import pyplot
x = numpy.arange(-9, 12)
pyplot.plot(x, x*x)
pyplot.show()


Try to skip the last point in the polygon at 
matplotlib/backends/backend_gdk.py#L101: 
self.gdkDrawable.draw_lines(gc.gdkGC, polygon[:-1])

<https://github.com/matplotlib/matplotlib/blob/v1.5.x/lib/matplotlib/backends/backend_gdk.py#L101>

Christoph




More information about the Matplotlib-users mailing list