[英]How to show tick labels in matplotlib histogram?
I do not usually use Matplotlib and I am trying to graph some information in a time series histogram.我通常不使用 Matplotlib,我试图在时间序列直方图中绘制一些信息。
My histogram works fine when I put the data in but I cannot get it to show the xaxis tick labels.当我放入数据时,我的直方图工作正常,但我无法让它显示 xaxis 刻度标签。
Here is my code (using placeholder data):这是我的代码(使用占位符数据):
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import HourLocator as HourLocater
import datetime
from datetime import datetime
from datetime import timedelta
now = datetime.today()
timecount = []
for x in range(8):
datea = now-timedelta(hours=x)
dateb = str(datea).split(':')[0]
timecount.append(dateb)
fig, ax = plt.subplots(1,1)
ax.hist(timecount, bins=50, color='lightblue')
ax.xaxis.set_major_locator(HourLocater(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H'))
plt.show()
You are creating x with string,then trying to format your axis with datetimes, Let's stick with datatimes, and use just datea
variable and not dateb
.您正在使用字符串创建 x,然后尝试使用日期时间格式化您的轴,让我们坚持使用数据时间,并仅使用datea
变量而不是dateb
。
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import HourLocator as HourLocater
import datetime
from datetime import datetime
from datetime import timedelta
now = datetime.today()
timecount = []
for x in range(8):
datea = now-timedelta(hours=x)
dateb = str(datea).split(':')[0]
timecount.append(datea)
fig, ax = plt.subplots(1,1)
ax.hist(timecount, bins=50, color='lightblue')
ax.xaxis.set_major_locator(HourLocater(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H'))
plt.xticks(rotation=90)
plt.show()
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.