Souvik Ray
Souvik Ray

Reputation: 3038

How to ensure even spacing between labels on x axis of matplotlib graph?

I have been given a data for which I need to find a histogram. So I used pandas hist() function and plot it using matplotlib. The code runs on a remote server so I cannot directly see it and hence I save the image. Here is what the image looks like

enter image description here

Here is my code below

import matplotlib.pyplot as plt

df_hist = pd.DataFrame(np.array(raw_data)).hist(bins=5) // raw_data is the data supplied to me
plt.savefig('/path/to/file.png')
plt.close()

As you can see the x axis labels are overlapping. So I used this function plt.tight_layout() like so

import matplotlib.pyplot as plt

df_hist = pd.DataFrame(np.array(raw_data)).hist(bins=5)
plt.tight_layout()
plt.savefig('/path/to/file.png')
plt.close()

There is some improvement now

enter image description here

But still the labels are too close. Is there a way to ensure the labels do not touch each other and there is fair spacing between them? Also I want to resize the image to make it smaller.

I checked the documentation here https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html but not sure which parameter to use for savefig.

Upvotes: 1

Views: 1536

Answers (2)

NAP_time
NAP_time

Reputation: 181

I think the two best fixes were mentioned by Pam in the comments. You can rotate the labels with plt.xticks(rotation=45 For more information, look here: Rotate axis text in python matplotlib

The real problem is too many zeros that don't provide any extra info. Numpy arrays are pretty easy to work with, so pd.DataFrame(np.array(raw_data)/1000).hist(bins=5) should get rid of three zeros off of both axes. Then just add a 'kilo' in the axes labels.

To change the size of the graph use rcParams.

from matplotlib import rcParams rcParams['figure.figsize'] = 7, 5.75 #the numbers are the dimensions

Upvotes: 1

jwalton
jwalton

Reputation: 5686

Since raw_data is not already a pandas dataframe there's no need to turn it into one to do the plotting. Instead you can plot directly with matplotlib.

There are many different ways to achieve what you'd like. I'll start by setting up some data which looks similar to yours:

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gamma

raw_data = gamma.rvs(a=1, scale=1e6, size=100)

If we go ahead and use matplotlib to create the histogram we may find the xticks too close together:

fig, ax = plt.subplots(1, 1, figsize=[5, 3])
ax.hist(raw_data, bins=5)
fig.tight_layout()

enter image description here

The xticks are hard to read with all the zeros, regardless of spacing. So, one thing you may wish to do would be to use scientific formatting. This makes the x-axis much easier to interpret:

ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0))

enter image description here

Another option, without using scientific formatting would be to rotate the ticks (as mentioned in the comments):

ax.tick_params(axis='x', rotation=45)
fig.tight_layout()

enter image description here

Finally, you also mentioned altering the size of the image. Note that this is best done when the figure is initialised. You can set the size of the figure with the figsize argument. The following would create a figure 5" wide and 3" in height:

fig, ax = plt.subplots(1, 1, figsize=[5, 3])

Upvotes: 1

Related Questions