How to combine two charts into the single PDF using ReportLab? - python

Question : I'm generating charts using ReportLab. Charts are generated properly but into different PDF. I want to combine them into a single existing pdf.
basic structure of code is
class BreakdownPieDrawing():
def firstChart():
#code for generating first Pie chart
def secondChart():
#code for generating second Pie chart
if __name__=="__main__":
drawing1 = BreakdownPieDrawing()
drawing1.firstChart()
drawing1.save(formats=['pdf'],outDir='.',fnRoot='first')
drawing2 = BreakdownPieDrawing()
drawing2.secondChart()
drawing2.save(formats=['pdf'],outDir='.',fnRoot='second')
for full code Snippets please refer http://www.reportlab.com/snippets/4/
This code produces two separate PDFs. How can i combine them into single PDF.
I tried this to code :
def makePdf(self,drawing):
doc = SimpleDocTemplate('hello.pdf')
doc.build(drawing)
and then after I'm passing "BreakdownPieDrawing" class's object into this method. But this approach is not working.
I'm new to reportLab and python so pardon me for such a ugly code.
So the question is how to add this charts into existing pdf. Any help would be greatly appreciated.

If you look closely at the code snippet you provided (disclaimer: I wrote ;-) ) specifically at line#33 you see
# adding a pie chart to the drawing
self._add(self,Pie(),name='pie',validate=None,desc=None)
That lines adds the Pie chart to the drawing, you will then need to add your second chart to the same drawing you use the _add method, again like the snippet does on line#46 for adding the legend. Your drawing will have the two charts and when you save it as PDF you should get them both.

Are these two charts actually full page? If so, you can just call for a page break between drawing the two. Otherwise you can just adjust the coordinates so that both will fit on the page.
Alternatively, if you are doing something more complicated and will end up using Platypus, it might make sense to create a custom Flowable to render them.

Related

Bubble chart data input in Python (dash_alternative_viz) with Highcharts

Last days I play around with Highcharts in Python Dash with dash_alternative_viz library.
Link: https://github.com/plotly/dash-alternative-viz
I can create various charts, but I have major problem with creating Bubble chart. I do not know how to input data to this kind of chart. I tried many ways but as a result I get error or empty chart.
Like here below simple bar chart:
Does anybody used this package and somehow generated the Bubble chart, and can share source code so I can see how to deliver input in right format.
Thanks,
Albert

Embedding Bokeh graphs while maintaining linked axis

I have linked the x axis of my bokeh graphs together by simply sharing the same x_range:
graph2.x_range = graph1.x_range
When I zoom-in/out or change the x-axis of one graph in any way, the other graph adjust with it. I really like this functionality.
However, when I embed the bokeh graphs in an html template they are not connected anymore.
script, div = components(graphs).
The components function returns a script that contains the data for your plot and provides a target div to display the plot view.
I added script and div in the placeholders of my html template, which is then loaded with Flask.
Everything works fine, but the graphs are not connected anymore.
It looks like the graps are embedded separately and therefore they cannot be linked together. I wonder if there is a method to do this.
If anyone has had this issue before, pls help :)
I used this documentation:
https://docs.bokeh.org/en/2.4.1/docs/user_guide/embed.html

Generate HTML with plotly graphs and a navbar

I am running a Python pipeline that generates some data files. As a last step of the pipeline, I want to generate a HTML file containing nicely styled graphs representing that data with plotly and a navbar at the top (I will probably have 2 tabs, each one containing different set of charts). This sounds like hard to do with plotly only. How can I achieve this? Should I make an html file with some styling, then just embed the plotly graphs in it? Or should I try to do it solely using plotly?
I did some something similar writing the html (and nav elements) from python and then embedding the charts using the to_html method:
fig.to_html(full_html=False, include_plotlyjs=True)

How can I arrange two faceted side-by-side charts horizontally in Altair?

Altair offers lovely feature to facet charts using facet method. For example, following dataset visualizes nicely:
print(df[['Year', 'Profile', 'Saison', 'Pos']].to_csv())
,Year,Profile,Saison,Pos
0,2017,6.0,Sommer,VL
1,2017,6.0,Winter,VL
13,2017,6.0,Winter,HL
12,2017,6.0,Sommer,HL
18,2017,6.0,Sommer,HR
6,2017,6.0,Sommer,VR
7,2017,6.0,Winter,VR
19,2017,6.0,Winter,HR
14,2018,5.5,Winter,HL
8,2018,5.5,Winter,VR
15,2018,5.5,Sommer,HL
20,2018,4.3,Winter,HR
21,2018,5.0,Sommer,HR
3,2018,5.5,Sommer,VL
2,2018,6.2,Winter,VL
9,2018,4.5,Sommer,VR
17,2019,4.5,Sommer,HL
11,2019,4.2,Sommer,VR
22,2019,3.5,Winter,HR
10,2019,5.28,Winter,VR
5,2019,4.6,Sommer,VL
4,2019,4.9,Winter,VL
16,2019,4.0,Winter,HL
23,2019,4.5,Sommer,HR
with the following command:
alt.Chart(df).mark_bar().encode(x='Year:O', y='Profile:Q').facet(row='Saison:N', column='Pos:N')
But, as you can seem I have still a lot of place horizontally and would like to use it by rearranging Winter plot right next to the Summer plot:
I understand that I already used column grid to facet over attribute Pos, but visually for me Winter and Sommer plots are two separate plots (just like here), which I'd like to place side by side.
I tried to create two different charts in the same cell and using html emit them side by side, but in Jupyter environment there is a limitation on just one Altair/Vega plot per cell.
Is there any method I can use to arrange these charts horizontally?
In Altair, there is no good way to do this, because faceted charts cannot be nested according to the Vega-Lite schema. However, the Vega-Lite renderer actually does handle this in some cases, despite it technically being disallowed by the schema.
So you can hack it by doing something like this:
chart = alt.Chart(df).mark_bar().encode(
x='Year:O',
y='Profile:Q'
).facet('Saison:N')
spec = alt.FacetChart(
data=df,
spec=chart,
facet=alt.Facet('Pos:N')
).to_json(validate=False)
print(spec)
The resulting spec can be pasted by hand into http://vega.github.io/editor to reveal this (vega editor link):
You'll even notice that the vega editor flags parts of the spec as invalid. This is admittedly not the most satisfying answer, but it sort of works.
Hopefully in the future the Vega-Lite schema will add actual support for nested facets, so they can be used more directly from Altair.

Writing a table to a matplotlib pdf file

I am using matplotlib and a modified version of this example to generate plots in pdf files. So I am plotting each plot on a single page and the results are just fine.
Now I would like to list all the data used in the plots in a rather long table. This table should be placed below the last plot (so not each plot should get its own table).
Is there a way to plot LaTeX like tables in a pdf file using matplotlib?
In principle, you can place almost any TeX stuff onto a plot using something like plt.text(1,2,r'$a^2+b^2=42$'). For aligning equations things like eqnarray work as well, like this. Just don't forget to use raw strings, for otherwise python can misinterpret TeX commands which start with backslashes.
Unless using a plot to write text, I think it is save to say, that it is not possible to only write a table to a matplotlib pdf output file.
Currently I am using tex to write the table and pyPdf to merge the two results. I think this is the cleanest solution to the problem.

Categories