Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pdf plots and account curve report #689

Closed
robcarver17 opened this issue Jul 14, 2022 Discussed in #680 · 0 comments
Closed

Create pdf plots and account curve report #689

robcarver17 opened this issue Jul 14, 2022 Discussed in #680 · 0 comments

Comments

@robcarver17
Copy link
Owner

Discussed in #680

Originally posted by robcarver17 July 12, 2022
It would be nice to have the option to output reports into a .pdf file. That way, figures can be plotted. The use case I'm thinking of is issue #653 but clearly one could use this for accounting p&l as well.

A quick check reveals that github.com can render .pdf so that is good.

The following code fragment seems the easiest way of generating .pdf files consisting of multiple plots (from https://datatofish.com/export-matplotlib-pdf/)

from matplotlib.backends.backend_pdf import PdfPages
Data1 = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }   
df1 = DataFrame(Data1,columns=['Unemployment_Rate','Stock_Index_Price'])

with PdfPages(r'C:\Users\Ron\Desktop\Charts.pdf') as export_pdf:
  
    plt.scatter(df1['Unemployment_Rate'], df1['Stock_Index_Price'], color='green')
    plt.title('Unemployment Rate Vs Stock Index Price', fontsize=10)
    plt.xlabel('Unemployment Rate', fontsize=8)
    plt.ylabel('Stock Index Price', fontsize=8)
    plt.grid(True)
    export_pdf.savefig()
    plt.close()

It also works on a headless terminal (checked).

It would be nice to include text as well, but let's park that for the moment

Now, this does not fit well into the paradigm used by the reporting code in psystemtrade. Remember, we generate a list of special objects (headers, body_text, table) which is then 'parsed' into a single text string, which we then email or dump to file etc.

So a better way is to output each plot into a single .pdf file and then use PyPDF2 to merge them together.

We could include text by using https://github.com/baruchel/txt2pdf

We create a special object type plot_figure which holds the temp filename, the title of the plot and nothing else (that gives forward compatibility to when we can use a mixture of graphs and text).

Upon parsing, if the plot only consists of figures, these are merged into a single temp pdf file

  • then if sent if to a file or email then the temp .pdf file is eithier renamed to the correct filename or the .pdf file is emailed as an attacment
  • If to a console, then we generate the temp .pdf and call evince to display (linux users only, windows will throw an error; this will also fail on a headless terminal for obvious reasons)

If the plot has a mixture of text and figures then, for now, an error is thrown (in the future we could convert the text to pdf files and then merge those).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant