1

Im new to matplotlib and wont to use the graphics in Latex. There ist a visual output as a graphic but:

Why is there no pdf output?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os #to remove a file
import datetime 
from matplotlib.backends.backend_pdf import PdfPages
#######################

Val1 = [1,2,3,4,5,6,7,8,9,9,5,5] # in kWh
Val2 = [159,77,1.716246,2,4,73,128,289,372,347,354,302] #in m³


index = ['Apr', 'Mai', 'Jun', 'Jul','Aug','Sep','Okt','Nov','Dez','Jan', 'Feb', 'Mrz']
df = pd.DataFrame({'Val1': Val1,'Val2': Val2}, index=index)



with PdfPages('aas2s.pdf') as pdf:

plt.rc('text', usetex=True)
params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage{amsmath}']}
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Liberation'
plt.rcParams.update(params)
plt.figure(figsize=(8, 6))
plt.rcParams.update({'font.size': 12}) 
ax = df[['Val1','Val2']].plot.bar(color=['navy','maroon'])

plt.xlabel('X Achse m')
plt.ylabel('Y Achse Taxi quer ')
plt.legend(loc='upper left', frameon=False)
plt.title('Franz jagt im komplett verwahrlosten Taxi quer durch Bayern')
plt.show()

pdf.savefig()
plt.close()

The error is called: ValueError: No such figure: None

And how do i get a second "Y" axis for the second value?

2
  • what do you want to obtain in the plot? Commented Jul 6, 2018 at 11:27
  • I think this line has a problem plt.rc('text', usetex=True)? Commented Jul 6, 2018 at 11:28

1 Answer 1

2

In general, savefig should be called before show. See e.g.

Second, you want to produce the plot inside the created figure, not create a new one, hence use

fig, ax = plt.subplots(figsize=...)
df.plot(..., ax=ax)

and later call the methods of the axes (object-oriented style).

In total,

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
#######################

Val1 = [1,2,3,4,5,6,7,8,9,9,5,5] # in kWh
Val2 = [159,77,1.716246,2,4,73,128,289,372,347,354,302] #in m³


index = ['Apr', 'Mai', 'Jun', 'Jul','Aug','Sep','Okt','Nov','Dez','Jan', 'Feb', 'Mrz']
df = pd.DataFrame({'Val1': Val1,'Val2': Val2}, index=index)



with PdfPages('aas2s.pdf') as pdf:

    plt.rc('text', usetex=True)
    params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage{amsmath}']}
    plt.rcParams['font.family'] = 'serif'
    plt.rcParams['font.serif'] = 'Times New Roman'
    plt.rcParams.update(params)

    fig, ax = plt.subplots(figsize=(8, 6))
    plt.rcParams.update({'font.size': 12}) 
    df[['Val1','Val2']].plot.bar(color=['navy','maroon'], ax=ax)

    ax.set_xlabel('X Achse m')
    ax.set_ylabel('Y Achse Taxi quer ')
    ax.legend(loc='upper left', frameon=False)
    ax.set_title('Franz jagt im komplett verwahrlosten Taxi quer durch Bayern')
    pdf.savefig()
    plt.show()
    plt.close()

Now if you still need to save the figure after is it being shown, you can do so by specifically using it as argument to savefig

plt.show()
pdf.savefig(fig)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.