Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib connecting wrong points in line graph

I am plotting two lists using matplotlib python library. There are two arrays x and y which look like this when plotted-

Click here for plot (sorry don't have enough reputation to post pictures here)

The code used is this-

import matplotlib.pyplot as plt
plt.plot(x,y,"bo")
plt.fill(x,y,'#99d8cp')

It plots the points then connects the points using a line. But the problem is that it is not connecting the points correctly. Point 0 and 2 on x axis are connected wrongly instead of 1 and 2. Similarly on the other end it connects points 17 to 19, instead of 18 to 19. I also tried plotting simple line graph using-

plt.plot(x,y)

But then too it wrongly connected the points. Would really appreciated if anyone could point me in right direction as to why this is happening and what can be done to resolve it.

Thanks!!

like image 273
Zeeshan Avatar asked Jul 27 '15 13:07

Zeeshan


People also ask

How to plot points in Matplotlib using Python?

Plotting of points in matplotlib with Python There is a method named as “ scatter (X,Y) ” which is used to plot any points in matplotlib using Python, where X is data of x-axis and Y is data of y-axis. Let’s understand this with some example:- In this example, we will plot only one point

How to connect scatterplot points with line in Matplotlib?

How to Connect Scatterplot Points With Line in Matplotlib? 1 Python3. import numpy as np. import matplotlib.pyplot as plt. x = [0.1, 0.2, 0.3, 0.4, 0.5] y = [6.2, -8.4, 8.5, 9.2, -6.3] plt.title ("Connected ... 2 Python3. 3 Python3.

How to plot multiple lines in a single chart in Matplotlib?

You can use pyplot’s xlabel () and ylabel () functions to set axis labels and use pyplot’s title () function to set the title for your chart. 3. Plot multiple lines in a single chart Matplotlib also allows you to plot multiple lines in the same chart. Generally used to show lines that share the same axis, for example, lines sharing the x-axis.

How do you plot a curve in Python?

Set the figure size and adjust the padding between and around the subplots. Define a draw_curve () method to make a curve with a mathematical expression. Plot point1 and point2 data points. Plot x and y data points returned from the draw_curve () method. To display the figure, use show () method.


Video Answer


1 Answers

The lines of matplotlib expects that the coordinates are in order, therefore you are connecting your points in a 'strange' way (although exactly like you told matplotlib to do, e.g. from (0,1) to (3,2)). You can fix this by simply sorting the data prior to plotting.

#! /usr/bin/env python
import matplotlib.pyplot as plt
x = [20, 21, 22, 23, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 17, 16, 19, 18]
y = [ 1,  1,  1,  1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,  2,  2,  2,  2,  2,  2,  2,  2,  1,  1]

x2,y2 = zip(*sorted(zip(x,y),key=lambda x: x[0]))

plt.plot(x2,y2)
plt.show()

That should give you what you want, as shown below:

enter image description here

like image 188
Bas Jansen Avatar answered Sep 27 '22 21:09

Bas Jansen