2014-09-19 2 views
0

для этого фрагмента кодаMatplotlib перекрытием несколько графиков в одном PDF

import matplotlib.pyplot as plt 
from matplotlib.backends.backend_pdf import PdfPages 

pp = PdfPages('long.pdf') 

lists = [ 
    ([1, 3, 6], ["bells", "whistles", "pasta"], 'title-1'), 
    ([11, 3, 6, 5], ["red", "blue", "green", "back"], 'title-2') 
] 

for x_list,label_list,title in lists: 
    plt.figure(3, figsize=(2,2)) 
    # plt.axes([0.1, 0.1, 0.8, 0.8]) 
    explode = (1, 0, 0, 0) 
    plt.axis('equal') 
    plt.pie(x_list, labels=label_list, autopct="%1.1f%%") 
    plt.title(title) 
    # plt.savefig(pp, format='pdf') 
    pp.savefig() 

pp.close() 

я получаю два (будет много в практической) графики в одном PDF, но второй из них выглядит немного внахлест на первый один

Я не могу разрешить это. любая помощь будет оценена по достоинству.

ответ

1

Потому что вы говорите ему, чтобы построить все круговые диаграммы друг над другом. plt.figure(3) всегда возвращает цифру с номером 3 и делает ее текущей фигурой. Команды pyplot затем отображают текущие оси.

# only make the figure and axes once 
fig, ax = plt.subplots(figsize=(2, 2)) 
# set the aspect to be equal 
ax.set_aspect('equal') 
# do the looping 
for x_list,label_list,title in lists: 
    # clear the axes 
    ax.cla() 
    # make the pie graph 
    ax.pie(x_list, labels=label_list, autopct="%1.1f%%") 
    # set the title 
    ax.set_title(title) 
    # save the figure 
    fig.savefig(pp, format='pdf') 
Смежные вопросы