question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to control the output figure size including legends and axes?

See original GitHub issue

When I set figure_size through theme or the options it seems like this setting only controls the figure itself, but does not take into account legends, axis, titles, etc.

Is there another setting that could set the size of the overall resulting figure?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
benjaminleroycommented, Apr 12, 2022

Hi there, I know this issue is a year old, but @wangmallory and I have been working on an extension to plotnine (cowpatch), and in this package we use an idea suggested in a blog post by Kavi Gupta (@kavigupta) in a backend to create correctly sized plotnine objects / matplotlib figures. 

The problem (love plotnine b.t.w.) is that plotnine’s underlying code leverages bbox_inches=“tight” from matplotlib. I do see that @has2k1 is looking into converting to plt.tight_layout() (issue #390).

A solution using our package would be something like the following:

import plotnine as p9
import cowpatch as cow 

# for reproducible example:
import plotnine.data as p9_data
g0 = p9.ggplot(p9_data.mpg) +\
    p9.geom_bar(p9.aes(x="hwy")) +\
    p9.labs(title = 'Plot 0')

out_info = cow.svg_utils._select_correcting_size_svg(g0, height = 4, width = 5, dpi = 96) #height and width in inches (in this example keep dpi = 96 )

# feed out_info[0], out_info[1] into p9.save’s height and width parameter
g0.save(filename = “local_image.pdf”, width = out_info[0], height = out_info[1])

The resulting file local_image.pdf (in whatever format you’d like) should be the desired size (up to some eps). For the above example, here is a screengrab of the size information from adobe acrobat:

Screen Shot 2022-04-10 at 10 10 31 AM

Note that this “solution” is an interative one, and sometimes (especially with large legends) this can still fail since the legend may constraint the minimum size your image can actually take.

1reaction
haggaiecommented, Jan 29, 2021

Thanks @himalayajung, though I don’t think that would help. What I want to accomplish it to set the size of the pdf plotnine would generate in advance, and have all the components fit that space. That makes it easier to them include the figure in a paper and keeping font and figure sizes uniform. I believe this is similar to the matplotlib tight layout feature, though I haven’t used it in a while, so I might be mistaken.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating figure with exact size and no padding (and legend ...
Something I can come up with is the following: It sets the text in figure coordinates and then resizes the axes either in...
Read more >
Change Axis Labels, Set Title and Figure Size to Plots with ...
functions to change axis labels and set the title for a plot. We can set the size of the text with size attribute....
Read more >
How to change figure size? - MATLAB Answers - MathWorks
The paper size options are for printing, so they don't change the size of the figure. The 'Position' property sets the size of...
Read more >
matplotlib - 2D and 3D plotting in Python
Great control of every element in a figure, including figure size and DPI. High-quality output in many formats, including PNG, PDF, SVG, EPS,...
Read more >
Advanced plotting — Python4Astronomers 2.0 documentation
defines two figures, one with two sets of axes, and one with one set of axes. ... you can control the font size...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found