Python Matplotlib打印时间重叠标签

Python Matplotlib打印时间重叠标签,python,python-3.x,matplotlib,plot,Python,Python 3.x,Matplotlib,Plot,所以我有两个值列表:一个是温度值(templist),一个是时间值(timelist) 我使用此代码绘制值: n = 0 bettertime = [] sizeoflist = len(timelist) while n < sizeoflist: newtime = datetime.strptime(timelist[n], "%H:%M:%S") bettertime.append(newtime.strftime("%H:%M")) n = n + 1

所以我有两个值列表:一个是温度值(templist),一个是时间值(timelist)

我使用此代码绘制值:

n = 0
bettertime = []
sizeoflist = len(timelist)
while n < sizeoflist:
    newtime = datetime.strptime(timelist[n], "%H:%M:%S")
    bettertime.append(newtime.strftime("%H:%M"))
    n = n + 1

fig, ax = plt.subplots()
ax.plot(bettertime, templist)
plt.show()
因此,经过一些研究,我发现我的时间列表(timelist)是一个字符串列表,而不是datetime格式的列表,因此matplotlib不知道如何缩小它

如何拟合图表中的值?(例如,圣殿骑士自动安装在Y型斧头上) (我想得到图表中的10个时间值,但从最大值到最小值,而不仅仅是列表中的前10个值)

作为旁注,列表(时间列表和圣堂列表)是随机长度的列表(长度相等,如果这很重要,len(时间列表)与len(时间列表)相等)

编辑:

解决方案:

fmt = mdates.DateFormatter('%H:%M')
bettertimelist = [datetime.strptime(i, '%H:%M:%S') for i in timelist]
fig.autofmt_xdate()
ax.xaxis.set_major_formatter(fmt)

可以指定旋转xtick标签,如下所示:

times = np.arange(10, 27, 1)
templist = np.sin(times)
bettertime = ["10:"+repr(time) for time in times] # make string for the xlabel, since that's what the question states

fig, ax = plt.subplots()
ax.plot(bettertime, templist)
plt.xticks(rotation=45)         # here's the line that does the rotation

您可以指定旋转xtick标签,如下所示:

times = np.arange(10, 27, 1)
templist = np.sin(times)
bettertime = ["10:"+repr(time) for time in times] # make string for the xlabel, since that's what the question states

fig, ax = plt.subplots()
ax.plot(bettertime, templist)
plt.xticks(rotation=45)         # here's the line that does the rotation

您可以尝试旋转xticks值。
plt.xticks(旋转=45)#90--->垂直xticks

n = 0
bettertime = []
sizeoflist = len(timelist)
while n < sizeoflist:
    newtime = datetime.strptime(timelist[n], "%H:%M:%S")
    bettertime.append(newtime.strftime("%H:%M"))
    n = n + 1

fig, ax = plt.subplots()
ax.plot(bettertime, templist)
plt.xticks(rotation=45)   # Add this line for xticks to be rotated
plt.show()
n=0
最佳时间=[]
sizeoflist=len(时间列表)
当n
您可以尝试旋转xticks值。
plt.xticks(旋转=45)#90--->垂直xticks

n = 0
bettertime = []
sizeoflist = len(timelist)
while n < sizeoflist:
    newtime = datetime.strptime(timelist[n], "%H:%M:%S")
    bettertime.append(newtime.strftime("%H:%M"))
    n = n + 1

fig, ax = plt.subplots()
ax.plot(bettertime, templist)
plt.xticks(rotation=45)   # Add this line for xticks to be rotated
plt.show()
n=0
最佳时间=[]
sizeoflist=len(时间列表)
当n
签出
pandas
。它提供了一些将表中的列转换为日期时间的简单方法,还允许您轻松地绘制表中的列。@Denziloe这是个好主意。但是我发现我可以用一个更简单的方法来解决这个问题。只需将数据从一个简单的字符串列表转换为datetime数组,matplotlib就会从中完成工作。请查看
pandas
。它提供了一些将表中的列转换为日期时间的简单方法,还允许您轻松地绘制表中的列。@Denziloe这是个好主意。但是我发现我可以用一个更简单的方法来解决这个问题。只需将数据从一个简单的字符串列表转换为datetime数组,matplotlib就会从中完成工作。这可能是一个解决方案,但如果我有一个只有5个数据的列表,我想这可能是一个解决方案,但如果我有一个只有5个数据的列表,我想这将是一个问题