Mplfinance: Multipage PDF

Created on 27 Jan 2021  路  3Comments  路  Source: matplotlib/mplfinance

Hi Daniel

Great work, this is an excellent upgrade, thank you for your hard work!

I'm new to programming but I implemented your new library into a program which creates a graph for each completion of my for-loop and then saves each time through the loop to create a multiple page pdf. This works with matplotlib creating a line chart but currently the pdf is always blank and I get this error in the terminal, "ValueError: No figure None". I'm assuming the way your library works, after creating and displaying a chart it wipes the data from memory once I close the chart being displayed on screen and therefore when it goes to save the pdf, there's nothing there? Is that correct?

Also, I don't suppose you have a suggestion of how to get around this? I wasn't sure if I would have to make use of the io.BytesIO buffer but I don't know anything about that just yet.

Many Thanks,
Matt

question

All 3 comments

Hi Matt,

Thanks for expressing your appreciation for and interest in mplfinance.

Without seeing your code, it's not clear to me how you got this to work with matplotlib line plots, nor how you are trying to make this work with mplfinance. Your hypothesis that the Figure goes out of scope (is deleted from memory) once it is saved to disk is reasonable, but may, or may not, be the case; again, depending on exactly how your are calling mplfinance: certainly you should not be getting "ValueError: No figure None".


That said, I think I can answer your question with a couple of examples. Grabbing the code from the top of the savefig example notebook and modifying to call mplfinance in a loop:

import pandas as pd
import mplfinance as mpf

df = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)

for jj in range(0,5):
    name = 'testsave' + str(jj) + '.pdf'
    mpf.plot(df[jj:jj+15],type='candle',volume=True,savefig=name)

The above code will create 5 different .pdf files each with a different plot.


To get the same 5 plots into a single .pdf file, do the following:

import pandas as pd
import mplfinance as mpf
from matplotlib.backends.backend_pdf import PdfPages

df = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)

with PdfPages('multipage_pdf.pdf') as pdf:
    for jj in range(0,5):
        fig, axlist = mpf.plot(df[jj:jj+15],type='candle',volume=True,returnfig=True)
        pdf.savefig(fig)

Notice, for the multipage .pdf we use returnfig instead of savefig, and directly use backend_pdf to save the Figure.

I hope that helps. All the best. --Daniel

Thanks so much Daniel, I really appreciate your detailed reply, I'll get chance over the weekend to implement it.
I know you don't need to spend your free time answering questions from beginners like me but I really appreciate that you did, all the best!

Thanks a lot for explaining it so clearly Daniel! I saved 44 charts manually today and decide to look for a better solution. Found your answer on StackOverflow and linked directly up to here! Wonderful job.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skfnxh picture skfnxh  路  3Comments

jainraje picture jainraje  路  4Comments

allahyarzadeh picture allahyarzadeh  路  3Comments

ismailbayram picture ismailbayram  路  4Comments

toksis picture toksis  路  5Comments