0

I'm working in a Jupyter notebook and trying to generate scatter plots (with best fit lines) of two variables for several values of a third categorical variable (around 350). Here's my code so far:

nrows = 20
ncols = 20

fig = plt.figure(figsize=(20,20), dpi=100)

for i, third_variable_value in enumerate(df.third_variable.unique()):
    ax = fig.add_subplot(nrows, ncols, i + 1)
    sns.regplot(data=df[df.third_variable == third_variable_value], x='first_variable', y='second_variable', ax=ax)
fig.tight_layout()
fig.savefig('test.jpg', dpi=400)

I've read through questions here and here about using an increased DPI as matplotlib requires the figure to fit to screen; and tight_layout() to prevent overlapping.

However, the saved figure has font that's too big in relation to the plot area, and the axes scale to become smaller but the points remain the same size. Is there a way to just shrink each subplot as a whole? Ideally, I'd like to keep the same aspect ratio for the plot area as a single figure and have the plot area to font size ratio be fixed.

These are two sample views of the saved file. enter image description here enter image description here

I know I can do it using something like sns.lmplot(data=df, x='first_variable', y='second_variable', col='third_variable', sharey=False, sharex=False, col_wrap=30) but I'd like to do it in the above manner as I eventually want to color each point by values of a fourth variable but with only one best fit line, and will be replacing the sns.regplot() with a matplotlib function.

Naumz
  • 481
  • 5
  • 15
  • I don't understand the question. Do you want to change the fontsize? "I want a prettier plot" is not really a question - people have different views on what looks pretty and what not. – ImportanceOfBeingErnest Nov 23 '17 at 11:51
  • I just clarified the question. Is there a way to just shrink each subplot as a whole? Ideally, I'd like to keep the same aspect ratio for the plot area as a single figure and have the plot area to font size ratio be fixed. Does that help? Thanks! – Naumz Nov 23 '17 at 17:17
  • If you keep the plot area vs. font size ratio fixed a larger plot will have a larger fontsize. Is that what you want? – ImportanceOfBeingErnest Nov 23 '17 at 20:06
  • Ideally, I'd like to decrease the font size and the size of the points in the image itself. For instance, when I use the `lmplot` syntax like so: `sns.lmplot(data=df, x='first_variable', y='second_variable', col='third_variable', sharey=False, sharex=False, col_wrap=30)` the plots generated all have a readable plot and the font size automatically shrinks in a figure with the same width when saved to disk. – Naumz Nov 24 '17 at 17:32
  • I guess you are aware of the png output also being scaled in the browser if it is larger than the notebook width. So the figure would need to know it is later being shown in a scaled fashion or not - which is rather cumbersome. Maybe you can provide a [mcve] of the issue, with one example showing the desired and one example showing the undesired output. – ImportanceOfBeingErnest Nov 25 '17 at 17:12

0 Answers0