[MRG+1] Fix title, overlapping plots and axis labels in plot_ols_ridge_variance.py#12296
Conversation
|
@qinhanmin2014 @ab-10 Please review this PR |
| @@ -47,25 +47,22 @@ | |||
| fig = plt.figure(fignum, figsize=(4, 3)) | |||
| @@ -47,25 +47,22 @@ | |||
| fig = plt.figure(fignum, figsize=(4, 3)) | |||
| plt.clf() | |||
There was a problem hiding this comment.
don't we need to clear the figure after we draw the figure? I'll experiment with both
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Hmm, I guess it's useless because it does not occur very often, but let's keep it here.
qinhanmin2014
left a comment
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Please fix this example here and start a new PR/issue for other examples. The first plot still has the same problem.
|
Thank you for the good catches @qinhanmin2014 |
rth
left a comment
There was a problem hiding this comment.
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])...
The issue more generally that as soon as you have multiple figures or subplots,
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 |
| ax.set_xlim(0, 2) | ||
| fignum += 1 | ||
|
|
||
| fig.tight_layout() |
There was a problem hiding this comment.
Notice here I use fig.tight_layout() to avoid labels being cropped. Is this method preferred or would subplots_adjust be better?
|
I think we can merge this one. It's not perfect but at least it works. |
|
Yes, it's much better, thanks @xhlulu ! |
|
@rth @qinhanmin2014 Thank you for the advice and guidance! |
Reference Issues/PRs
Fixes #12224
What does this implement/fix? Explain your changes.
I removed
plt.axes, and replacedaxwithplt. 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:

