简体   繁体   English

matplotlib:重新缩放轴标签

[英]matplotlib: Rescale axis labels

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 . 我用刻度sklearn.preprocessing按比例绘制在X轴和Y轴,其压缩我的数据到我的数据-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: 看起来像:

情节

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 我真正想要的是一天中的小时数xlabels为0-> 24(最小值为0),ylabels为0-> 600

My first answer is: just keep a copy of your original data. 我的第一个答案是:仅保留原始数据的副本。 Much the simplest, most pythonic answer. 最简单,最Python化的答案。

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() . 使用StandardScaler()代替scale() You can either inverse_transform() the data when you are done using the scaled data: 使用缩放数据完成后,可以对数据进行inverse_transform()

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: 或使用比例因子创建x_ticks和x_ticklabel:

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. 对于错别字我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM