简体   繁体   English

在 python 的 matplotlib.pyplot 中编辑 X 轴

[英]Editing X-axis in matplotlib.pyplot for python

I cannot seem to get the X-labels for the X-axis spaced out properly.我似乎无法正确间隔开 X 轴的 X 标签。 A picture is given below.下面给出一张图片。 I need to understand how to set x-axis label distance and the data for x-axis is time series in hours and minutes.我需要了解如何设置 x 轴 label 距离,x 轴的数据是以小时和分钟为单位的时间序列。




title = "Energy plots for " + escalators[0].split(".")[0]
label = 'kWh'






xe = plt.figure(figsize=(30, 15))
plt.title(title)
plt.ylabel(label)
plt.plot(date_time, y, 'kp-', markersize=3, linewidth=0.5)


ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter('hh:mm:ss'))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
#ax.xaxis.set_major_locator(mdates.DateLocator(interval=2))
plt.gcf().autofmt_xdate()
plt.xticks(date_time)
plt.rc('xtick', labelsize=12)
plt.subplots_adjust(bottom = 0.1)

You could try setting the minimum, maximum, and incrementt, using np.arange您可以尝试使用np.arange设置最小值、最大值和增量

plt.xticks(np.arange(datetime(1985,7,1), datetime(2015,7,1), timedelta(days=1)).astype(datetime))

This would give you the x-axis in whatever range and interval you want.这将为您提供所需范围和间隔的 x 轴。

Don't forget that you need to import from datetime import datetime, timedelta不要忘记需要from datetime import datetime, timedelta

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

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