Aren Mark Boghozian
Aren Mark Boghozian

Reputation: 159

Matplotlib x-axis overlap

I have two lists, x_axis which is list of time in the format of '12:30:00'. The y-axis is percent values. I need to plot all the values on a graph, however since x-axis string is too long they overlap. Is there anyway I can have matplotlib not show every single time on x-axis? Any help would be appreciated. enter image description here

enter image description here

Upvotes: 12

Views: 36608

Answers (4)

Nannigalaxy
Nannigalaxy

Reputation: 617

I needed to step x axis digits instead of rotating.

ax.set_xticks(np.arange(0, max_number, 5)) #step 5 digits

Output:matplotlib plot

Upvotes: 5

Ruzbeh Irani
Ruzbeh Irani

Reputation: 2438

One way to do this automatically is by using autofmt_xdate

fig.autofmt_xdate():

for getting fig object you will have to call the subplot functions

fig, ax = plt.subplots()

Works really well

enter image description here

Upvotes: 3

Sara
Sara

Reputation: 322

You can rotate your label to show the list time using the below code.

plt.xticks(rotation=90)

Upvotes: 8

Scott Boston
Scott Boston

Reputation: 153460

You could rotate and print every 2nd ticklabel:

_ = plt.plot(df['str_time'], df.Pct, 'ro')
ax = plt.gca()
plt.axis([0,24,0,50])
plt.xticks(rotation=90)
for label in ax.get_xaxis().get_ticklabels()[::2]:
    label.set_visible(False)

Output:

enter image description here

Upvotes: 19

Related Questions