Tinku
Tinku

Reputation: 51

Setting markers fro scatter plot

I wanted to set markers for scatter plot as shown belowenter image description here

I am using matplotlib library for plotting until now I am able to plot only the points by the code

import matplotlib.pyplot as plt
import numpy as np
x = [39.5,38,42.5]
y = np.array([0,1,2])
my_xticks = ['a','b','c']
plt.yticks(y, my_xticks)
plt.scatter(x, y,marker='x',s=100)

plt.show()

I also want the line as shown in figure above as a marker in my plot

Upvotes: 0

Views: 65

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

One way to do this would be to call plt.hlines, so that you can add a few horizontal lines to your markers.

Example:

import matplotlib.pyplot as plt
import numpy as np
x = np.array([39.5,38,42.5])
y = np.array([0,1,2])
my_xticks = ['a','b','c']
plt.yticks(y, my_xticks)
plt.scatter(x, y,marker='x',s=100)
width = .4
plt.hlines(y, xmin=x - width/2, xmax=x + width/2)
plt.show()

Change width to suit your desired "width" for each line. Colors can be changed as well, check the documentation for plt.hlines.

Note that this will not persist in a call to legend. legend will only use the actual line objects, although there are ways to change that too.

Upvotes: 3

Related Questions