带日期和时间的reportlab LinePlot轴 - python

我想使用reportlab可视化LinePlot中的数据。数据具有x轴值(时间戳),格式为YYYYMMDDHHMMSS。我知道存在一个reportlab x轴类NormalDateXValueAxis,但它仅带有日期(YYYYMMDD),并且不允许使用时间。

一个问题是,reportlab是否已经使用我尚未找到的任何类支持此功能?

我尝试的另一种方法是简单地将时间戳字符串用作x轴值,并为这些值定义一个格式化程序。一个例子是:

from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
from datetime import datetime


def formatter(val):
    dtstr = str(int(val))
    print(dtstr)
    dt = (datetime.strptime(str(int(val)), "%Y%m%d%H%M%S")).strftime("%d.%m.%Y %H:%M:%S")
    return dt

class Test(_DrawingEditorMixin, Drawing):

    def __init__(self,width=258,height=150,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        # font
        fontSize = 7
        # chart
        self._add(self,LinePlot(),name='chart',validate=None,desc=None)
        self.chart.y                = 16
        self.chart.x                = 32
        self.chart.width            = 212
        self.chart.height           = 90
        # x axis
        self.chart.xValueAxis.labels.fontSize       = fontSize-1
        self.chart.xValueAxis.labelTextFormat       = formatter
        # y axis
        self.chart.yValueAxis.labels.fontSize       = fontSize -1
        # sample data
        self.chart.data = [
            [
                (20200225130120, 100),
                (20200225130125, 0),
                (20200225130130, 300),
                (20200225130135, 0),
                (20200225130140, 500),
                (20200225130145, 0),
                (20200225130150, 700),
                (20200225130155, 0),
                (20200225130315, 900)
            ]
        ]

if __name__=="__main__": #NORUNTESTS
    Test().save(formats=['pdf'],outDir='.',fnRoot=None)

但是我有两个问题。

给格式化程序的值是不可预测的(至少对我而言)。 Reportlab似乎以它认为最好的方式修改了刻度线。结果有时是有些值不是有效的时间戳,并且不能由datetime解析。有时我会感到例外,即秒数必须在0到59之间。Reportlab创建了一个值为20200225136000的刻度。
由于x轴不知道这些值是时间戳,因此它仍然为2020022513596120200225135965等留有空间。结果是图中的间隙。

带日期和时间的reportlab LinePlot轴 - python

python大神给出的解决方案

一个问题是reportlab是否已经支持任何类的此功能
我还没有找到?

我不知道,但是我想可以从ValueAxis实现您想要的。如果您可以更改库,建议您使用matplotlib,就像我看到的previous working examples一样。您还可以尝试查看PYX(这也是ReportLab的一个很好的替代方法)是否可以应对这种情况,但是我没有找到任何解决方案。

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…

Python:检查新文件是否在文件夹中[重复] - python

This question already has answers here: How do I watch a file for changes? (23个答案) 3年前关闭。 我是python的新手,但是我尝试创建一个自动化过程,其中我的代码将侦听目录中的新文件条目。例如,某人可以手动将zip文件复制到一个特定的文件夹中,并且我希望我的代码能够在文件完全…