Skip to content

ENH: Add the ability to have a separate title for each subplot when plotting #14753

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

Merged
merged 15 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^

- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)

- ``pd.DataFrame.plot`` now prints a title above each subplot if ``suplots=True`` and ``title`` is a list of strings
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add this PR number in the issue tag (:issue:14753)

Copy link
Author

Choose a reason for hiding this comment

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

Done :)


.. _whatsnew_0200.api_breaking:

Expand Down
23 changes: 20 additions & 3 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works,
_ok_for_gaussian_kde)


""" Test cases for misc plot functions """


@tm.mplskip
class TestSeriesPlots(TestPlotBase):

def setUp(self):
TestPlotBase.setUp(self)
import matplotlib as mpl
Expand Down Expand Up @@ -54,7 +52,6 @@ def test_bootstrap_plot(self):

@tm.mplskip
class TestDataFramePlots(TestPlotBase):

@slow
def test_scatter_plot_legacy(self):
tm._skip_if_no_scipy()
Expand Down Expand Up @@ -277,6 +274,26 @@ def test_radviz(self):
handles, labels = ax.get_legend_handles_labels()
self._check_colors(handles, facecolors=colors)

@slow
def test_subplot_titles(self):
df = self.iris.drop('Name', axis=1).head()
# Use the column names as the subplot titles
title = list(df.columns)

# Case len(title) == len(df)
plot = df.plot(subplots=True, title=title)
self.assertEqual([p.title._text for p in plot], title)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I missed this earlier, but can you use p.get_title() instead of the private matplotlib ._text method. Same thing down on line 300, ax.get_title().

Copy link
Author

Choose a reason for hiding this comment

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

Oh cool I didn't know that existed! Just pushed with the change.


# Case len(title) > len(df)
self.assertRaises(ValueError, df.plot, subplots=True,
title=title + ["kittens > puppies"])

# Case len(title) < len(df)
self.assertRaises(ValueError, df.plot, subplots=True, title=title[:2])

# Case subplots=False and title is of type list
self.assertRaises(ValueError, df.plot, subplots=False, title=title)


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
22 changes: 19 additions & 3 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,22 @@ def _adorn_subplots(self):

if self.title:
if self.subplots:
self.fig.suptitle(self.title)
if is_list_like(self.title):
if len(self.title) != len(self.axes):
Copy link
Member

Choose a reason for hiding this comment

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

pls use self.nseries. Number of columns and axes and can differ when layout is specified.

Also add test case with 3 numeric columns df with df.plot(subplots=True, layout=(2, 2))

Copy link
Author

Choose a reason for hiding this comment

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

Done :)

msg = 'The length of `title` must equal the number ' \
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to report the length of title and the expected number of columns in the error message.

Copy link
Author

Choose a reason for hiding this comment

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

Done :)

'of columns if using `title` of type `list` ' \
'and `subplots=True`'
raise ValueError(msg)

for (ax, title) in zip(self.axes, self.title):
ax.set_title(title)
else:
self.fig.suptitle(self.title)
else:
if is_list_like(self.title):
msg = 'Using `title` of type `list` is not supported ' \
'unless `subplots=True` is passed'
Copy link
Member

Choose a reason for hiding this comment

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

same here

raise ValueError(msg)
self.axes[0].set_title(self.title)
Copy link
Member

Choose a reason for hiding this comment

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

should raise if subplots=False and input is list-like


def _apply_axis_properties(self, axis, rot=None, fontsize=None):
Expand Down Expand Up @@ -2555,8 +2569,10 @@ def _plot(data, x=None, y=None, subplots=False,
figsize : a tuple (width, height) in inches
use_index : boolean, default True
Use index as ticks for x axis
title : string
Title to use for the plot
title : string or list
If a string is passed, print the string at the top of the figure. If a
Copy link
Member

Choose a reason for hiding this comment

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

Can you leave the starting sentence "Title to use for the plot" ?

list is passed and subplots is True, print each item in the
Copy link
Member

Choose a reason for hiding this comment

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

single backticks (`) around 'subplots' (to make it clear that it is a keyword)

list above the corresponding subplot.
grid : boolean, default None (matlab style default)
Axis grid lines
legend : False/True/'reverse'
Expand Down