matplotlib: Rescale axis labels

2.8k views Asked by At

I've used scale from sklearn.preprocessing to scale my data on the X and Y axis which compressed my data to -2 < x < 2. When I plot this data, I want the original scaling back for use on the tick marks.

My code looks like:

scale(readings_array, copy=False)
plt.plot(readings_array)
ax = plt.gca()
ax.set_xticklabels(np.arange(0,24))
plt.ylabel("Total Traffic Volume")
plt.xlabel("Time")
plt.show()

Which looks like:

plot

What I really want is for the the xlabels to be 0->24 (0 at the smallest value) for hours of the day and the ylabels to be 0->600

1

There are 1 answers

1
Andreus On BEST ANSWER

My first answer is: just keep a copy of your original data. Much the simplest, most pythonic answer.

scaled_array = scale(readings_array, copy=True)
# do stuff like learning with scaled_array
plt.plot(readings_array)

If you are trying to avoid making copies of your data. use StandardScaler() instead of scale(). You can either inverse_transform() the data when you are done using the scaled data:

scaler = sklearn.preprocessing.StandardScaler(copy=False)
readings_array = scaler.fit_transform( readings_array )
# do stuff with scaled data
readings_for_plotting = scaler.inverse_transform( readings_array )

or use the scaling factors to create x_ticks and x_ticklabels:

my_xtick_labels = np.arange(0,25)
my_xticks = (my_xticks*scaler.std_) + scaler.mean_
plt.set_xticks( my_xticks )
plt.set_xticklables( my_xtick_labels )

With my apologies for typos.