Skip to content Skip to sidebar Skip to footer

Plot A List Of Tuples On X Axis

I've data like the following x = [(1,0), (0, 1), (2, 1), (3, 1)] y = [1, 4, 8.5, 17.5] and I would like to plot in the following way But matplotlib considers x as a 2 column vect

Solution 1:

You can also convert the tuples to strings and plot a categorical scatter:

plt.scatter([str(i) for i in x], y)

enter image description here

Solution 2:

You can use set_xticks and set_xticklabels methods of an Axes object

x = [(1,0), (0, 1), (2, 1), (3, 1)]
y = [1, 4, 8.5, 17.5]

fig,ax = plt.subplots()
ax.scatter(range(len(y)),y)
ax.set_xticks(range(len(y)))
ax.set_xticklabels([str(item) for item in x]);

The output figure is

output

Solution 3:

I am not sure for what are you using but so try this way (create subarrays and add zero as second element.

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn; seaborn.set()
x = [[1,0], [0, 1], [2, 1], [3, 1]]
y = [[1,0], [4,0], [8.5,0], [17.5,0]]
plt.scatter(x,y)
plt.show()

you willl get something like this

enter image description here

Post a Comment for "Plot A List Of Tuples On X Axis"