Rubicelia Patlán
Rubicelia Patlán

Reputation: 15

X-ticks overlapping with Matplotlib

I have a script for plotting big data sets. I have a problem while setting the xticks in my plot. I have tried the following code:

plt.xticks(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data,rotation=90, fontsize= 12

The problem is that I have more than 2000 data points for x and the ticks get overlapped. I want to have ticks at every 5th data point. I have tried using np.arange as:

plt.xticks(np.arange(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data,rotation=90, fontsize= 12

but it plots the first 50 data points along the plot and not the corresponding ones. Any idea how to solve this?

Upvotes: 1

Views: 245

Answers (1)

Sheldore
Sheldore

Reputation: 39042

Currently you are using the whole data for setting the x-ticklabels. The first argument to the xticks() function is the location of the ticks and the second argument is the tick labels.

You need to use indexing to get every 5th data point (corresponding to the ticks). You can access it using [::5]. So you need to pass data[::5] to your xticks() as

plt.xticks(np.arange(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data[::5],rotation=90, fontsize= 12)

You can also use range() as

plt.xticks(range(min(configDBcomplete.index),max(configDBcomplete.index[-1]),5),data[::5],rotation=90, fontsize= 12)

Upvotes: 1

Related Questions