python - 2 轴 Reportlab 图

标签 python graph reportlab

我通过重叠条形图和线条图,在 ReportLab 中成功创建了一个 2 轴图。以下是任何对类似事物感兴趣的人的代码:

from reportlab.graphics.shapes import Drawing,colors
from reportlab.graphics.widgets.markers import makeMarker
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.charts.lineplots import LinePlot

drawing = Drawing(400, 200)
data = [(13, 5, 20, 22, 37, 45, 19, 4)
        ]
noOfBars=len(data[0])

bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data

bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50        
bc.categoryAxis.categoryNames = ['Jan-99','Feb-99','Mar-99','Apr-99','May-99','Jun-99','Jul-99','Aug-99']
drawing.add(bc)

data3=[[(0.5, 4), (1.5, 3), (2.5, 4), (3.5, 6), (4.5, 4), (5.5, 2), (6.5, 5), (7.5, 6)]
       ]

lp = LinePlot()
lp.x = bc.x
lp.y = bc.y
lp.height = bc.height
lp.width = bc.width
lp.data = data3
lp.joinedLines = 1
lp.lines[0].symbol = makeMarker('Circle')
lp.lines[0].strokeColor=colors.blue
lp.lineLabelFormat = '%2.0f'
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax = noOfBars
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 8
lp.xValueAxis.visible=False
lp.yValueAxis.visible=False #Hide 2nd plot its Yaxis
drawing.add(lp)

y2Axis = YValueAxis()#Replicate 2nd plot Yaxis in the right
y2Axis.setProperties(lp.yValueAxis.getProperties())
y2Axis.setPosition(lp.x+lp.width,lp.y,lp.height)
y2Axis.tickRight=5
y2Axis.tickLeft=0

y2Axis.configure(data3)
y2Axis.visible=True
drawing.add(y2Axis)

renderPDF.drawToFile(drawing, '../tmp/barline.pdf')

唯一的问题是第二个有指向图表内部的标签。我希望它们是 outside图!我找到了一个调整刻度的属性(y2Axis.tickRight=5,y2Axis.tickLeft=0),但 yvalue 标签位置仍然是个谜!

提前致谢

最佳答案

我最近做了这个。有点像

y2Axis.labels.dx = 10

应该能帮到你。

关于python - 2 轴 Reportlab 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1826761/

相关文章:

python - 动态报告的 Django 和 reportlab 问题

python-3.x - 使用 ReportLab 有条件地为表格单元格着色

python - 使用 pip3 问题安装 guppy

python - 在没有多重索引的情况下分割 Pandas 系列

python - 使用 Python 获取视频中的 I 帧列表

php - 如何在图表中显示一周的每一天数据?

algorithm - 使用链表和矩阵的图形表示

python - msgpack 解压每个项目之间的数字 '10'

c++ - 查找图中由一个顶点分隔的顶点

python - 如何使用 Reportlab 将生成的 PDF 保存到 App Engine Python 中的数据存储区