gpt4 book ai didi

python - 将日期标签添加到 reportlab LinePlot 图表

转载 作者:太空狗 更新时间:2023-10-30 02:34:29 29 4
gpt4 key购买 nike

我正在使用 reportlab 生成 LinePlot 图表。我似乎无法获得 X 轴的非数字标签。 enter image description here

有没有人有什么想法?

这是我的 Lineplot 图表类(注意:我在这个类之外进行了一些计算和设置,但你明白了要点

import reportlab
from advisor.charting.Font import Font

from reportlab.lib.colors import Color, HexColor
from reportlab.lib.pagesizes import cm, inch
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics.charts.linecharts import HorizontalLineChart
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.shapes import Drawing, String, _DrawingEditorMixin
from reportlab.graphics.widgets.markers import makeMarker


class TacticalAugLineGraph(_DrawingEditorMixin, Drawing):
def __init__(self, width=100, height=110, legend=False, *args, **kw):
apply(Drawing.__init__, (self, width, height) + args, kw)

chartFont = Font('Gotham-Bold')

self._add(self, LinePlot(), name='chart', validate=None, desc=None)
self.chart._inFill = 1
self.chart.x = 20
self.chart.y = 15
self.chart.width = 85
self.chart.height = 95

#self.chart.lineLabelFormat = '%d%%'
self.chart.yValueAxis.valueMin = 0
self.chart.yValueAxis.valueMax = 100
self.chart.yValueAxis.valueStep = 10

def apply_colors(self, colors):

self.chart.lines[0].strokeColor = colors[0]
self.chart.lines[1].strokeColor = colors[1]
self.chart.lines[2].strokeColor = colors[2]
self.chart.lines[3].strokeColor = colors[3]

最佳答案

我制作了一个您可以测试的简单示例。结果可能如下所示: enter image description here

#!/usr/bin/python
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.shapes import Drawing
from reportlab.lib import colors
from random import randint
from datetime import date, timedelta

# Generate some testdata
data = [
[(x,randint(90,100)) for x in range(0,2001,100)],
[(x,randint(30,80)) for x in range(0,2001,100)],
[(x,randint(5,20)) for x in range(0,2001,100)],
]

# Create the drawing and the lineplot
drawing = Drawing(400, 200)
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp._inFill = 1
lp.data = data
for i in range(len(data)):
lp.lines[i].strokeColor = colors.toColor('hsl(%s,80%%,40%%)'%(i*60))

# Specify where the labels should be
lp.xValueAxis.valueSteps = [5, 500, 1402, 1988]
# Create a formatter that takes the value and format it however you like.
def formatter(val):
#return val
#return 'x=%s'%val
return (date(2010,1,1) + timedelta(val)).strftime('%Y-%m-%d')

# Use the formatter
lp.xValueAxis.labelTextFormat = formatter
drawing.add(lp)

from reportlab.graphics import renderPDF
renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates')

在格式化程序中有 2 个可选的 return 语句,您可以使用它们来更好地掌握它。

当然,如果 x 轴上的数据是以日期开头,则不需要任何格式化程序,在这种情况下,您可以只指定标签的位置(如果您对默认位置不满意)。

上面的例子借鉴了 reportlab documentation 中第 105 页的想法。 .

祝你好运:)

关于python - 将日期标签添加到 reportlab LinePlot 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8082803/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com