简体   繁体   English

MatPlotLib X轴与日期

[英]MatPlotLib X axis with dates

I have a graph within a GUI which opens with a button click. 我在GUI中有一个图形,单击该图形即可打开。 It opens from a csv file that constantly updates, hence the counting at the beginning, I wanted to limit the number of entries for the x-axis. 它从一个不断更新的csv文件打开,因此从一开始的计数开始,我想限制x轴的条目数。

The plot works for the four lines however, I have been unable to refine the x axis, what I would like to do is; 该图适用于四行,但是我无法优化x轴,我想做的是;

  1. Rotate the font 旋转字体
  2. Remove the trailing zeros for the dates 删除日期的尾随零
  3. Possibly Change the Date Format to DMY H:M 可能将日期格式更改为DMY H:M

The Below script is what I have so far: 以下脚本是我到目前为止所拥有的:

def open_graph():
    import numpy as np
    import matplotlib.pyplot as plt
    import datetime as dt 
    from dateutil import parser
    from csv import reader
    with open('Voltage_Readings.csv','r') as f:
        data = list(reader(f))

    file=open('Voltage_Readings.csv')
    numlines=(len(file.readlines())-1)
    start=numlines-100
    end=numlines

    fig=plt.figure()
    ax=plt.subplot(111)

    DTG = [parser.parse(i[1]) for i in data[start:end]]
    Battery_Level = [i[2] for i in data[start:end]]
    Gene_Supply = [i[3] for i in data[start:end]]
    Solar_Supply = [i[4] for i in data[start:end]]
    Wind_Supply = [i[5] for i in data[start:end]]

    plt.plot(DTG, Battery_Level,'-r', label='Battery')
    plt.plot(DTG, Gene_Supply,'-y', label='Gene')
    plt.plot(DTG, Solar_Supply,'-b', label='Solar')
    plt.plot(DTG, Wind_Supply,'-g', label='Wind')

    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width *0.85, box.height])
    ax.legend(bbox_to_anchor=(1, 1),loc=2)

    plt.title('VOLTAGE MEASUREMENTS FROM POWER SOURCES')
    plt.xlabel('Time')
    plt.ylabel('Voltage')

    plt.show()

I would appreciate any assistance is being able to achieve this as I am still learning. 我将不胜感激,尽管仍在学习中,但能够提供帮助。

Here is a better answer: 这是一个更好的答案:

Just add this before your plt.show() command: 只需在您的plt.show()命令之前添加以下内容:

fig.autofmt_xdate()
import matplotlib.dates as mdates
ax.fmt_xdata = mdates.DateFormatter('%d-%m-%Y %H:%M')
datetimefmt = mdates.DateFormatter("%d-%m-%Y %H:%M")
ax.xaxis.set_major_formatter(datetimefmt)

Running autofmt_xdate makes the dates slanted. 运行autofmt_xdate会使日期倾斜。 Setting ax.fmt_xdata sets the date format when you hover your mouse over a point. 将鼠标悬停在某个点上时,设置ax.fmt_xdata会设置日期格式。 Running ax.xaxis.set_major_formatter sets the date format on the x axis. 运行ax.xaxis.set_major_formatter设置x轴上的日期格式。

Interestingly enough in my own matplotlib code I've done my own formatting of the dates on the x axis because I wasn't aware of these auto formatting features. 有趣的是,在我自己的matplotlib代码中,我已经对x轴上的日期进行了自己的格式化,因为我不知道这些自动格式化功能。 I'm going to have to revise my code to use what's already in matplotlib. 我将不得不修改我的代码以使用matplotlib中已经存在的代码。

Bobby 鲍比

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

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