Skip to content

[MRG+1] Fix title, overlapping plots and axis labels in plot_ols_ridge_variance.py#12296

Merged
qinhanmin2014 merged 6 commits intoscikit-learn:masterfrom
xhluca:xhlulu-fix-plot-ols-ridge-va
Oct 9, 2018
Merged

[MRG+1] Fix title, overlapping plots and axis labels in plot_ols_ridge_variance.py#12296
qinhanmin2014 merged 6 commits intoscikit-learn:masterfrom
xhluca:xhlulu-fix-plot-ols-ridge-va

Conversation

@xhluca
Copy link
Copy Markdown
Contributor

@xhluca xhluca commented Oct 5, 2018

Reference Issues/PRs

Fixes #12224

What does this implement/fix? Explain your changes.

I removed plt.axes, and replaced ax with plt. The resulting plot does not have the odd overlap between the title and the plot. It also fixes the axis range.

Any other comments?

Below are what they look like with the new code:
figure_1
figure_2

@xhluca
Copy link
Copy Markdown
Contributor Author

xhluca commented Oct 5, 2018

@qinhanmin2014 @ab-10 Please review this PR

Copy link
Copy Markdown
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -47,25 +47,22 @@
fig = plt.figure(fignum, figsize=(4, 3))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fig is not used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true!

@@ -47,25 +47,22 @@
fig = plt.figure(fignum, figsize=(4, 3))
plt.clf()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we need to clear the figure after we draw the figure? I'll experiment with both

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it doesn't really make a difference whether plt.clf() is used or not (tested both on notebook and python 3.6). I presume it might make a difference when using older versions of pyplot, but that be hard to test since I don't know which version the original tutorial was written in.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess it's useless because it does not occur very often, but let's keep it here.

Copy link
Copy Markdown
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @xhlulu

Copy link
Copy Markdown
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, the label on x-axis is overlapping with the code. You might refer to other similar examples to fix it.
See https://35569-843222-gh.circle-artifacts.com/0/doc/auto_examples/linear_model/plot_ols_ridge_variance.html

plt.xlim(0, 2)
fignum += 1

plt.tight_layout()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok figured out the error. Basically by default the x axis label is too low, so it is partially cropped out of the picture generated. However, when you set tight_layout() it rescales the plot layout to correctly show the labels.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that problems happens in other examples too, including this one: http://scikit-learn.org/dev/auto_examples/applications/plot_out_of_core_classification.html#plot-results

Should I start a separate PR addressing the issue, or should I address it in this one?

Thank you so much for guiding me through this PR! It's my second time contribution, first time outside of documentation so I'm still new to this :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix this example here and start a new PR/issue for other examples. The first plot still has the same problem.

Copy link
Copy Markdown
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @xhlulu

@qinhanmin2014 qinhanmin2014 changed the title Fix title, overlapping plots and axis labels in plot_ols_ridge_variance.py [MRG+1] Fix title, overlapping plots and axis labels in plot_ols_ridge_variance.py Oct 6, 2018
@xhluca
Copy link
Copy Markdown
Contributor Author

xhluca commented Oct 6, 2018

Thank you for the good catches @qinhanmin2014

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switches this example back to the legacy matplotlib API. Defining axis and calling ax.plot instead of plt.plot is now the recommended way. See for instance #11564

There should be a way to fix the overlap of the title while keeping axis. I would start with,

fig, ax = plt.subplots(1, 2, figsize=figsize)

then if necessary increase the spacing between the title and the plot as suggested in this SO answer. It shouldn't be necessary though since you don't do it here, and it renders fine. The key point may be to remove plt.axes([.12, .12, .8, .8])...

@xhluca
Copy link
Copy Markdown
Contributor Author

xhluca commented Oct 7, 2018

@rth The examples described in #11564 create images using subplots, and warns about plt.subplot old behavior being deprecated. This examples generates two different plots (without subplots) and show them to the user.

@rth
Copy link
Copy Markdown
Member

rth commented Oct 7, 2018

This examples generates two different plots (without subplots) and show them to the user.

The issue more generally that as soon as you have multiple figures or subplots, plt.plot will plot implicitly in one of them depending on the context (or the current figure state), that is bad and for this reason the explicit ax.plot is preferred even if you have 2 figures with 1 subplot each -- that way you can actually track where each plot should be made. See matplotlib docs,

In general, try to use the object-oriented interface over the pyplot interface.

My point that this is orthogonal to the things you are aiming to fix. It is possible to fix title and axis label overlap without reverting to the matplotlib pyplot API inherited from Matlab...

@xhluca
Copy link
Copy Markdown
Contributor Author

xhluca commented Oct 7, 2018

This examples generates two different plots (without subplots) and show them to the user.

The issue more generally that as soon as you have multiple figures or subplots, plt.plot will plot implicitly in one of them depending on the context (or the current figure state), that is bad and for this reason the explicit ax.plot is preferred even if you have 2 figures with 1 subplot each -- that way you can actually track where each plot should be made. See matplotlib docs,

In general, try to use the object-oriented interface over the pyplot interface.

My point that this is orthogonal to the things you are aiming to fix. It is possible to fix title and axis label overlap without reverting to the matplotlib pyplot API inherited from Matlab...

Thank you for the links and detailed explanation. It makes a lot of sense now. I will revert back to the old OOP method, but swap out plt.plot with ax.plot

ax.set_xlim(0, 2)
fignum += 1

fig.tight_layout()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice here I use fig.tight_layout() to avoid labels being cropped. Is this method preferred or would subplots_adjust be better?

@qinhanmin2014
Copy link
Copy Markdown
Member

I think we can merge this one. It's not perfect but at least it works.

Copy link
Copy Markdown
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @xhlulu

@qinhanmin2014 qinhanmin2014 merged commit 5df8cd3 into scikit-learn:master Oct 9, 2018
@rth
Copy link
Copy Markdown
Member

rth commented Oct 9, 2018

Yes, it's much better, thanks @xhlulu !

@xhluca
Copy link
Copy Markdown
Contributor Author

xhluca commented Oct 9, 2018

@rth @qinhanmin2014 Thank you for the advice and guidance!

jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the plot in plot_ols_ridge_variance.py

3 participants