My use requires the AX feature not PLT (most answers ive found focused on PLT which does NOT have the same method calls)
What I'm looking for is to be able to organize the x-values... A) in order by A, B, C, D, E, F B) to display A and C though they have no data points associated with it
I'm sure its something simple I'm missing... Here's a test example of it
import matplotlib.pyplot as plt
#desired x axis labels
x_label_order = ["A","B","C","D","E","F"]
#data to be plotted
x=["B","E","D","F"]
y=[1,2,3,4]
#creates graph
fig, ax = plt.subplots (1,1)
ax.scatter(x,y)
#assuming this is where changes need to be made?
ax.set_xticks(x_label_order)
ax.set_xticklabels(x_label_order)
plt.show()
Welcome to Stack Overflow.
For a Scatterplot, typically both x
and y
are numerical. But matplotlib
is smart (or not) enough to convert the string labels to running numbers automatically.
If you want to preserve the sequence, when you plot, you need to convert x
to its corresponding index location first:
x_label_order = ["A","B","C","D","E","F"]
x = ["B","E","D","F"]
x = [x_label_order.index(group) for group in x]
Then after you plot, set xticks
at the numerical location, and pass the labels to it:
ax.set_xticks(list(range(len(x_label_order))))
ax.set_xticklabels(x_label_order)
and you should get what you require: