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

Conversation

icanhazcodeplz-zz
Copy link

@icanhazcodeplz-zz icanhazcodeplz-zz commented Nov 26, 2016

  • [] tests added / passed
  • [] passes git diff upstream/master | flake8 --diff
  • [] whatsnew entry

@codecov-io
Copy link

codecov-io commented Nov 27, 2016

Current coverage is 85.26% (diff: 0.00%)

Merging #14753 into master will decrease coverage by 0.01%

@@             master     #14753   diff @@
==========================================
  Files           144        144          
  Lines         50968      50977     +9   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          43466      43467     +1   
- Misses         7502       7510     +8   
  Partials          0          0          

Powered by Codecov. Last update 3ac41ab...59ab880

@@ -1217,7 +1217,11 @@ def _adorn_subplots(self):

if self.title:
if self.subplots:
self.fig.suptitle(self.title)
if type(self.title) == list:
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 is_list_like, and check the input length are correct. Note that all the axes may not require title when you specify layout

Copy link
Author

Choose a reason for hiding this comment

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

Ok I'll use is_list_like. I purposely did not check the lengths as a feature. As it's written, if you only provide a list of two strings, but the plot has 3 suplots, the first two subplots will have a title and the third will be left without one.

@sinhrks
Copy link
Member

sinhrks commented Nov 27, 2016

Thx for the PR. can u add tests and release note? Also, pls link the original issue if we have.

@icanhazcodeplz-zz
Copy link
Author

I did my best at adding a test and release note. I didn't see an original issue that this addresses. It's just something I have on my local that I wanted to contribute.

self.assertEqual([p.title._text for p in plot], title)

# Case len(title) > len(df)
plot = df.plot(subplots=True, title=title + ['Ignore me!'])
Copy link
Member

Choose a reason for hiding this comment

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

I think we should raise if length of passed list and number of axes are different.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good I added exceptions and pushed. Is ValueError appropriate here?

for (ax, title) in zip(self.axes, self.title):
ax.set_title(title)
else:
self.fig.suptitle(self.title)
else:
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

-raise ValueError if subplots=False and title is of type list
@icanhazcodeplz-zz
Copy link
Author

Is there anything else I need to do for this change to get pulled in?

@@ -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 :)

@sinhrks
Copy link
Member

sinhrks commented Dec 6, 2016

@TomAugspurger pls have a look when you have a time.

@@ -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 :)

self.fig.suptitle(self.title)
if is_list_like(self.title):
if len(self.title) != len(self.axes):
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 :)

-print out length of title and number of columns in error message
-print out length of title and number of columns in error message
-added test case for when layout=(2,2) but number of columns=3
# Conflicts:
#	doc/source/whatsnew/v0.20.0.txt

# 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.

@TomAugspurger
Copy link
Contributor

Great thanks. +1 for merge when travis passes. Just ping us if you notice that it's green before we do

@icanhazcodeplz-zz
Copy link
Author

All green :)

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

@bmagnusson few more minor (stylistic) comments

msg = 'The length of `title` must equal the number ' \
'of columns if using `title` of type `list` ' \
'and `subplots=True`.\n' \
'length of title = {}\n' \
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 use parentheses around the full string instead of \ for the line continuation? (for consistency within the project)

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

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
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)

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" ?

…ing.

-A few other stylistic changes for consistency.
@icanhazcodeplz-zz
Copy link
Author

Huh. I assume there is something funny going on since I didn't change any actual code. Is there a way to restart the checks?

@icanhazcodeplz-zz
Copy link
Author

Ah! I figured it out. I had a linting error (I'll start doing lint before committing oops!)

@icanhazcodeplz-zz
Copy link
Author

All green now! Anything else to change? If not, thanks for all the help everyone! I had fun. This was my first pull request but it certainly won't be the last :).

@jorisvandenbossche jorisvandenbossche added this to the 0.20.0 milestone Dec 9, 2016
@jorisvandenbossche jorisvandenbossche merged commit 5f057cb into pandas-dev:master Dec 9, 2016
@jorisvandenbossche
Copy link
Member

@bmagnusson Thanks! Looking forward to more PRs :-)

yarikoptic added a commit to neurodebian/pandas that referenced this pull request Dec 12, 2016
* commit 'v0.19.0-174-g81a2f79': (156 commits)
  BLD: escape GH_TOKEN in build_docs
  TST: Correct results with np.size and crosstab (pandas-dev#4003) (pandas-dev#14755)
  Frame benchmarking sum instead of mean (pandas-dev#14824)
  CLN: lint of test_base.py
  BUG: Allow TZ-aware DatetimeIndex in merge_asof() (pandas-dev#14844)
  BUG: GH11847 Unstack with mixed dtypes coerces everything to object
  TST: skip testing on windows for specific formatting which sometimes hangs (pandas-dev#14851)
  BLD: try new gh token for pandas-docs
  CLN/PERF: clean-up of the benchmarks (pandas-dev#14099)
  ENH: add timedelta as valid type for interpolate with method='time' (pandas-dev#14799)
  DOC: add section on groupby().rolling/expanding/resample (pandas-dev#14801)
  TST: add test to confirm GH14606 (specify category dtype for empty) (pandas-dev#14752)
  BLD: use org name in build-docs.sh
  BF(TST): use = (native) instead of < (little endian) for target data types (pandas-dev#14832)
  ENH: Introduce UnsortedIndexError  GH11897 (pandas-dev#14762)
  ENH: Add the ability to have a separate title for each subplot when plotting (pandas-dev#14753)
  DOC: Fix grammar and formatting typos (pandas-dev#14803)
  BLD: try new build credentials for pandas-docs
  TST: Test pivot with categorical data
  MAINT: Cleanup pandas/src/parser (pandas-dev#14740)
  ...
yarikoptic added a commit to neurodebian/pandas that referenced this pull request Dec 12, 2016
release 0.19.1 was from release branch

* releases: (156 commits)
  BLD: escape GH_TOKEN in build_docs
  TST: Correct results with np.size and crosstab (pandas-dev#4003) (pandas-dev#14755)
  Frame benchmarking sum instead of mean (pandas-dev#14824)
  CLN: lint of test_base.py
  BUG: Allow TZ-aware DatetimeIndex in merge_asof() (pandas-dev#14844)
  BUG: GH11847 Unstack with mixed dtypes coerces everything to object
  TST: skip testing on windows for specific formatting which sometimes hangs (pandas-dev#14851)
  BLD: try new gh token for pandas-docs
  CLN/PERF: clean-up of the benchmarks (pandas-dev#14099)
  ENH: add timedelta as valid type for interpolate with method='time' (pandas-dev#14799)
  DOC: add section on groupby().rolling/expanding/resample (pandas-dev#14801)
  TST: add test to confirm GH14606 (specify category dtype for empty) (pandas-dev#14752)
  BLD: use org name in build-docs.sh
  BF(TST): use = (native) instead of < (little endian) for target data types (pandas-dev#14832)
  ENH: Introduce UnsortedIndexError  GH11897 (pandas-dev#14762)
  ENH: Add the ability to have a separate title for each subplot when plotting (pandas-dev#14753)
  DOC: Fix grammar and formatting typos (pandas-dev#14803)
  BLD: try new build credentials for pandas-docs
  TST: Test pivot with categorical data
  MAINT: Cleanup pandas/src/parser (pandas-dev#14740)
  ...
ischurov pushed a commit to ischurov/pandas that referenced this pull request Dec 19, 2016
…lotting (pandas-dev#14753)

* Add logic such that if 'title' is a list and 'subplots' is True, use each item of the list as the title of the individual subplots.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants