2轴Reportlab图 [英] 2 Axes Reportlab Graph

查看:150
本文介绍了2轴Reportlab图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功在ReportLab中创建2轴图形,方法是重叠条形图和线条。以下是对类似情况感兴趣的任何人的代码:

  from reportlab.graphics.shapes import绘图,颜色为
reportlab.graphics.widgets.markers从reportlab.graphics.charts.barcharts导入makeMarker
从reportlab.graphics.charts.lineplots导入VerticalBarChart
导入LinePlot

drawing =绘图(400 ,200)
data = [(13,5,20,22,37,45,19,4)
]
noOfBars = len(数据[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 ''''''''''''''''''''''''''''''''''''''''''''''''''''
drawing.add(bc)$ b (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绘制其Yaxis
drawing.add(lp)

y2Axis = YValueAxis()#在右边复制第二个图Yaxis
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')

唯一的问题是第二个标签指向图的内部。我希望他们能够外部图表!!我发现了一个属性来调整蜱(y2Axis.tickRight = 5,y2Axis.tickLeft = 0),但是yvalue标签的位置还是个谜!

/ p>

解决方案

我最近做了这个。类似于

  y2Axis.labels.dx = 10 

应该为您解决问题。


I have managed to create a 2 axes graph in ReportLab, by overlapping a barchart and linepot. Here is the code for anyone interested in something similar:

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')

The only problem is that the 2nd has labels pointing inside the graph. I would like them to be outside the graph!. I found a property to adjust the ticks (y2Axis.tickRight=5 , y2Axis.tickLeft=0) but the yvalue labels position is yet a mystery!

Thanks in advance

解决方案

I did this recently. Something like

y2Axis.labels.dx = 10

should do the trick for you.

这篇关于2轴Reportlab图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆