-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TST: fix warnings on multiple subplots #37274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TST: fix warnings on multiple subplots #37274
Conversation
Tests related to datetime failed in in Linux py38_locale. Should be unrelated to the topic. |
looks fine to me. @charlesdong1991 @simonjayhawkins @WillAyd if any comments. |
Can you merge master again? Is the Windows failure not related? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the code here. Is there a solution without so much code duplication?
I can extract portions of the code from |
is adding an extra keyword argument to _check_plot_works not an option? |
I tried to implement a parameter |
Tests on windows failed now because of internal CI problem. |
# UserWarning: To output multiple subplots, | ||
# the figure containing the passed axes is being cleared | ||
# Similar warnings were observed in GH #13188 | ||
axes = _check_plot_works(df.hist, column=column, layout=(1, 3)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emm, will _check_single_plot_works
not raise warning here? seems the purpose of having _check_single_plot_works
is to avoid creating subplots inside function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, _check_single_plot_works
does not raise the warning concerned, regarding figure being cleared.
In my opinion the need for _check_single_plot_works
(or whatever the better name would be) is when the plotting method (like DataFrame.hist
) creates subplots itself (may depend on the the options provided to the method).
The warning gets raised when the plotting method creates enough subplots required on its own, but we forcefully add subplots in _check_plot_works
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emm, okay. for DataFrame.hist
, i think as long as it plots data from multiple columns, or a by
is used, then will have subplots.
my main concern is in pandas.plot
API where the implementation is different, and there is an argument called subplots
, then do we also need to use _check_single_plot_works
(or probably other name is better) when subplots=True
for those test cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of putting in-line comments under _check_plot_works
function, maybe it's nicer to put few words about usecase/guideline of when to suggest to use which so that could help future contributors select the right function? how does this sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of putting in-line comments under
_check_plot_works
function, maybe it's nicer to put few words about usecase/guideline of when to suggest to use which so that could help future contributors select the right function? how does this sound?
I actually thought of not using comments here and instead call _check_single_plot_works
. This way there will be no warning raised.
axes = _check_single_plot_works(df.hist, column=column, layout=(1, 3))
result = [axes[0, i].get_title() for i in range(3)]
assert result == expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emm, okay. for
DataFrame.hist
, i think as long as it plots data from multiple columns, or aby
is used, then will have subplots.my main concern is in
pandas.plot
API where the implementation is different, and there is an argument calledsubplots
, then do we also need to use_check_single_plot_works
(or probably other name is better) whensubplots=True
for those test cases?
Right, is subplots=True
, then it seems that we need to use _check_single_plot_works
.
But I really don't like the name of this function )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way there will be no warning raised.
yes, #37274 (comment) actually I was here wondering why not use _check_single_plot_works
to remove warning
I actually thought of not using comments
sorry for my confusing comment, i meant to have one sentence to tell future users when to use _check_single_plot_works
and when to use _check_plot_works
in testing, something like Using _check_single_plot_works when plotting methods generate a subplot itself, if not, use _check_plot_works
will be enough IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I really don't like the name of this function
indeed, maybe _check_plot_with_subplots_works
? or something more straightforward/explicit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about _check_plot_default_axes_works
?
I replaced catching warning with the use of the new function as well.
I do not like _check_plot_with_subplots_works
either because it is not clear what the word subplots means here.
In fact original _check_plot_works
creates subplots, so one can easily mix-up the meaning of the subplots term.
@ivanovmg can you merge master and update to comments |
we need to combine these check_plot_works to a single function (add an arg to differentiate them) |
def _gen_default_plot(f, fig, **kwargs): | ||
""" | ||
Create plot in a default way. | ||
""" | ||
yield f(**kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here fig
is a dummy variable.
We have it here to ensure the same function signature with _get_two_subplots
.
Alternatively, we can create empty figure in both generator functions and yield fig, f(**kwargs)
.
It seemed to me that yielding one argument rather than two is clearer, thus I did it the way it is now.
I figured out how to implement it with an optional keyword argument. |
lgtm @charlesdong1991 @WillAyd if any comments |
would be nicer to put a sentence in docs to briefly explain the new added kwarg a bit but also okay if merge ^^ |
I added the docstring. Please take a look. |
great work! @ivanovmg |
thanks @ivanovmg |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
This PR suggests a (partial) fix for such warnings, which take place when running test suite on
pandas/tests/plotting
.The problem is the following.
The function
_check_plot_works
creates subplot(211) if axes are not provided.This is fine, when the plot to be created is a single-axes plot.
However, if the plot itself is to be plotted on multiple axes (for instance, df.hist would plot each column on a dedicated subplot, by default), then the warnings mentioned are emitted.
Two ways to handle it:
_check_plot_works
: do not create subplot(211).I created
_check_single_plot_works
which does not create subplots and used it in the problematic cases.I guess, that the name is not the best (I would prefer
_check_plot_works
), but I encourage those concerned to have a discussion here.Please see comments in
_check_plot_works
(I will remove them once we come to the conclusion on the topic).