问题
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 vector so the y data is plotted twice.
Suggestions on how to generate a plot in the above-mentioned format will be really helpful.
回答1:
You can also convert the tuples to strings and plot a categorical scatter:
plt.scatter([str(i) for i in x], y)
回答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
回答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
来源:https://stackoverflow.com/questions/64980924/plot-a-list-of-tuples-on-x-axis