简体   繁体   中英

Save Crosstab as PDF - Python

I have created a crosstab with Matplotlib, however when trying to save it with plt.savefig, I only get a blank picture as PDF output.

How do I save my crosstab plot as a PDF?

  df = credit_cards_avg  #df is just a data frame

    crosstab = pd.crosstab(df[attribute], df['credit_default'])
    # Normalize the cross tab to sum to 1:
    crosstab_normalized = crosstab.div(crosstab.sum(1).astype(float), axis=0)

    crosstab_normalized.plot(kind='bar', stacked=True, 
                   title='Default by ' + str(attribute) + ' class')

    plt.savefig("/home/daniele/dm-local/ps-default-class.pdf") #outputs blank file

The documentation says that there is a 'format' argument of savefig()

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html

Try this and see what happens

plt.savefig("/home/daniele/dm-local/ps-default-class.pdf", format='pdf')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM