Mike Ulm
Mike Ulm

Reputation: 390

pandas.plot and pyplot.save_fig create different sized PNGs for same figsize

When I call the same function that uses pandas.plot with the same figsize, I get different sized PNG files. The width is same but the height in pixels changes. I suspect that the length of the x-axis labels changes the height.I have not yet tried directly calling the matplotlib functions.

I have also tried plt.rcParams['figure.figsize'] = (7,4). The problem does not appear to be in how figsize is set. My print_fig_info always produces the desire values.

# Primitive way that confirmed that the figure size does not change
def print_fig_info(label=""):
    print(label,str(plt.gcf().get_size_inches()))

def my_plot(df):
    global c
    print_fig_info("Before plot")
    df.plot(kind='bar', figsize=(7,4))
    print_fig_info("After  plot")

    # want to make output files unique
    c += 1
    plt.savefig("output"+str(c), bbox_inches='tight', dpi='figure')

Upvotes: 0

Views: 173

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339590

In your call to savefig you explicitely ask matplotlib to change the figsize to the minimal size that still fits all the elements in via bbox_inches='tight'. Or in other words, bbox_inches='tight' is especially designed for changing the figure size to the minimum bounding box, and matplotlib is therefore doing what it's being asked for.

Solution: Don't use bbox_inches='tight'.

Upvotes: 2

Related Questions