Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dashed lines from points to axes in matplotlib

I have a set of points which I am plotting like this:

import matplotlib.pyplot as plt`

x = [1,2,3,4,5,6]
y = [1,4,9,16,25,36]

plt.scatter(x, y)

plt.show()

This gives output like this

enter image description here

What I want is to drop perpendiculars from the points to the axes, like in the figure below:

enter image description here

How can this be achieved?

like image 364
Damitr Avatar asked Oct 06 '17 05:10

Damitr


People also ask

How do I make a dashed line in Matplotlib?

x: X-axis points on the line. y: Y-axis points on the line. linestyle: Change the style of the line.

What does %Matplotlib mean in Python?

What Does Matplotlib Mean? Matplotlib is a plotting library available for the Python programming language as a component of NumPy, a big data numerical handling resource. Matplotlib uses an object oriented API to embed plots in Python applications.


1 Answers

Using hlines and vlines you can plot horizontal and vertical lines respectively.

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [1,4,9,16,25,36]

plt.vlines(x, 0, y, linestyle="dashed")
plt.hlines(y, 0, x, linestyle="dashed")
plt.scatter(x, y, zorder=2)

plt.xlim(0,None)
plt.ylim(0,None)
plt.show()

enter image description here

like image 68
ImportanceOfBeingErnest Avatar answered Oct 01 '22 12:10

ImportanceOfBeingErnest