Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib阶梯轴标签_Python_Matplotlib - Fatal编程技术网

Python matplotlib阶梯轴标签

Python matplotlib阶梯轴标签,python,matplotlib,Python,Matplotlib,我正在使用matplotlib在tkinter应用程序中生成图像。数据提供了一个按时间存储的值(实时应用程序数据以秒为单位打开会话以显示负载)。我试图在x轴上显示时间,在y轴上显示会话 但是,我在x轴上格式化标签时遇到了一些困难。如果您看到图像测试1,如果我将整数指定给x轴,则尽管数据为0-19,matplotlib会自动显示0,5,10,15,以保持轴整洁 如果我只是将标签指定给它,那么将显示前4个标签,而不是每5个标签显示一个。如果我重置TCIK和标签,我会得到每一个记号和每一个标签(这正好

我正在使用matplotlib在tkinter应用程序中生成图像。数据提供了一个按时间存储的值(实时应用程序数据以秒为单位打开会话以显示负载)。我试图在x轴上显示时间,在y轴上显示会话

但是,我在x轴上格式化标签时遇到了一些困难。如果您看到图像测试1,如果我将整数指定给x轴,则尽管数据为0-19,matplotlib会自动显示0,5,10,15,以保持轴整洁

如果我只是将标签指定给它,那么将显示前4个标签,而不是每5个标签显示一个。如果我重置TCIK和标签,我会得到每一个记号和每一个标签(这正好适合这里,但在我真正的应用程序中有太多的数据)

我的解决方案是手动计算每个第n个记号和第n个标签,并分配那些有效的值,但似乎应该有一些内置的功能来处理此类数据,与处理整数的方式相同

我的代码是:

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
import datetime
import tkinter

def main():

    x_labels = []
    x = []
    y = []

    dt_now = datetime.datetime.now()
    entries = 20
    for i in range(0, entries):
        newtime=(dt_now + datetime.timedelta(seconds=i)).strftime("%H:%M:%S")
        x.append(i)
        y.append(i/2)
        x_labels.append(newtime)


    root = tkinter.Tk()
    fig = plt.figure(figsize=(8,8))
    canvas = FigureCanvasTkAgg(fig, master=root) 
    canvas.draw()

    ax1 = fig.add_subplot(221)
    ax1.plot(x,y)
    ax1.set_title('Test 1 - Original Values')
    ax1.set_xlabel('Entry ID')
    ax1.set_ylabel('Sessions')

    ax2 = fig.add_subplot(222)
    ax2.plot(x,y)
    ax2.set_title('Test 2 - Labels 0,1,2,3\nExpecting labels 0,5,10,15')
    ax2.set_xlabel('Entry Time')
    ax2.set_ylabel('Sessions')
    ax2.set_xticklabels(x_labels, rotation=90, ha='center')

    ax3 = fig.add_subplot(223)
    ax3.plot(x_labels,y)
    ax3.set_title('Test 3 - Every label')
    ax3.set_xlabel('Entry Time')
    ax3.set_ylabel('Sessions')
    ax3.set_xticklabels(x_labels, rotation = 90)

    major_ticks = []
    major_tick_labels = []
    for i in range(0,entries,int(entries/5)):
        major_ticks.append(x[i])
        major_tick_labels.append(x_labels[i])

    ax4 = fig.add_subplot(224)
    ax4.plot(x,y)
    ax4.set_title('Test 4 - What I''m expecting\nbut hard coded')
    ax4.set_xlabel('Entry Time')
    ax4.set_ylabel('Sessions')
    ax4.set_xticks(major_ticks)
    ax4.set_xticklabels(major_tick_labels, rotation=90, ha='center')

    plt.subplots_adjust(hspace = 0.75, bottom = 0.2)
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

    tkinter.mainloop()


if __name__ == '__main__':
    main()
这将生成以下内容:

这是实现要求的唯一方法,还是我缺少一些可用的东西。我已经阅读了文档,但在那里看不到任何相关内容(过去我确保将记号和标签设置在一起)。我希望该过程尽可能自动化,因为时间框架将由用户驱动,因此最好使用4个刻度、5个刻度、10个刻度等。Matplotlib似乎能够很好地处理定义的整数范围内的这一要求,我只想关联相同的标签。

您可以使用。请注意,您需要将x_标签的格式设置为datetimes,这样才能工作(我删除了您对.strftime的调用)

这会给你


非常感谢。这正是我要找的。
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x_labels = []
x = []
y = []

dt_now = datetime.datetime.now()
entries = 20
for i in range(0, entries):
    newtime=(dt_now + datetime.timedelta(seconds=i))
    x.append(i)
    y.append(i/2)
    x_labels.append(newtime)

fig = plt.figure(figsize=(8,8))

ax4 = fig.add_subplot(224)
ax4.plot(x_labels,y)
ax4.set_title('One possible solution')
ax4.set_xlabel('Entry Time')
ax4.set_ylabel('Sessions')
ax4.xaxis.set_major_locator(mdates.SecondLocator(interval=4))
ax4.tick_params(axis="x", rotation=90)