Skip to content

API: dedicated plot methods like plot.scatter as an alternative to using kind #9124

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

Closed
shoyer opened this issue Dec 21, 2014 · 17 comments · Fixed by #9321
Closed

API: dedicated plot methods like plot.scatter as an alternative to using kind #9124

shoyer opened this issue Dec 21, 2014 · 17 comments · Fixed by #9321
Milestone

Comments

@shoyer
Copy link
Member

shoyer commented Dec 21, 2014

Currently, we use the plot for every possible kind of plot, and switch between them using the kind keyword argument.

From an API design and discoverability perspective, this seems less than ideal. If we had separate methods, standard introspection tools like IPython and autocomplete could help users figure out which arguments are applicable for which type of plot with less reliance on the documentation.

Another option is to make a .plot also an accessor, so we could have df.plot.scatter and so forth. But I suspect .plot_scatter would be a bit more discoverable.

@dalejung
Copy link
Contributor

I'm +1 on the accessor. We might want to group additional methods/attributes that don't fit the plot_ prefix. I recall there being an issue about grouping the already large pandas global namespace into logical groups.

@shoyer shoyer changed the title Add dedicated plot methods like plot_scatter instead of using kind? API: Add dedicated plot methods like plot_scatter or plot.scatter instead of using kind? Dec 21, 2014
@shoyer
Copy link
Member Author

shoyer commented Dec 21, 2014

@jorisvandenbossche
Copy link
Member

I agree with the sentiment of plot being too overloaded, and by having all the different plot types in one function making this a huge function. Also from a documentation point of view it is very difficult to document all the parameters, as a lot of plot kinds have specific ones only for that plot type.

But, the latest evolution in pandas was actually in the opposite direction. As lately, the separate hist and boxplot methods have also been added to the plot kind interface. See eg #7809

@shoyer
Copy link
Member Author

shoyer commented Dec 21, 2014

@jorisvandenbossche I think it was a good idea to move hist and boxplot to plot kind purely from a consistency perspective -- these methods were hard to find on their own without a common accessor or prefix. But the common prefix or accessor would be even better, and of course we would keep .plot(kind=...) deprecated (or maybe only discouraged) for quite a long time before removing it.

@jreback
Copy link
Contributor

jreback commented Dec 21, 2014

plot_* methods are pretty inflexible from s user perspective

not easy to switch bar to line or whatever and really hard to do programmatically (you have to compose the method name)

so big -1 on that

the accessor is a bit more amenable as it then allows kw separation and tab completion

should only be 1 obvious method of doing this

.plot(kind=...) seems intuitive to me

@shoyer
Copy link
Member Author

shoyer commented Dec 21, 2014

Perhaps we could just consolidate plotting methods based on their API.

Off the top of my head:

category kinds
1d line, bar, area, pie?
density hist, kde/density
1d/density box (violin?)
2d scatter, hexbin (hist2d?)

From a programmatic perspective, it's nice to be able to use kind to toggle between sub-kinds of the same category (e.g., line to bar), but not between categories.

@jreback
Copy link
Contributor

jreback commented Dec 21, 2014

@shoyer that looks reasonable

@shoyer
Copy link
Member Author

shoyer commented Dec 21, 2014

That said, I do still think dedicated methods/accessor would be a better API. Even within a category, there are plenty of kind specific options.

On Sun, Dec 21, 2014 at 2:51 PM, jreback notifications@github.com wrote:

@shoyer that looks reasonable

Reply to this email directly or view it on GitHub:
#9124 (comment)

@dalejung
Copy link
Contributor

I wonder if making plot both a callable and accessor is an option? plot() would work the same but also have methods with plot-type specific docs and call signatures i.e. plot.scatter. I would assume the methods would just wrap around plot.

@shoyer
Copy link
Member Author

shoyer commented Dec 22, 2014

I wonder if making plot both a callable and accessor is an option? plot() would work the same but also have methods with plot-type specific docs and call signatures i.e. plot.scatter. I would assume the methods would just wrap around plot.

Yes, this is not a bad option, although ultimately it would probably be cleaner to have the base plot method wrap the specific types, e.g.,

class SeriesPlotter(object):
    def __call__(self, kind='line', **kwargs):
        return getattr(self, kind)(**kwargs)

"only one obvious way to do it" is great, but pandas leans a bit more toward practicality.

@onesandzeroes
Copy link
Contributor

I like the idea of having specific plot functions attached to plot, so plot.scatter, plot.bar, while still being able to call plot(kind=...). Would allow specific docstrings (excellent for knowing exactly which keyword args are relevant to the current type), tab completion etc.

If we could divide the plot types into categories like shoyer suggested that would be good too, but the proposed categories don't seem all that obvious/intuitive to me. I wouldn't have thought of a bar chart as a 1D plot (although it makes sense now that I think about it), and isn't line is either 1D or 2D depending on whether we're plotting with the index as x-axis or using a second variable? Would probably create a decent amount of confusion for people who know which kind of plot they want to find their way through to it.

@shoyer
Copy link
Member Author

shoyer commented Jan 3, 2015

It sounds like there is support for preserving the kind argument to plot, while also making plot an accessor with more specific sub-methods?

@dalejung
Copy link
Contributor

dalejung commented Jan 3, 2015

+1. I like the idea of typing plot.<TAB> and getting a list of the plotting universe.

@TomAugspurger
Copy link
Contributor

I'm Ok with this too. As far as implementation goes, do we have a df/series.plot callable that dispatches off the kind argument and then add the different .bar, .line ... methods as attributes? Do we have any examples of similar methods?

On Jan 3, 2015, at 11:02, Dale Jung notifications@github.com wrote:

+1. I like the idea of typing plot. and getting a list of the plotting universe.


Reply to this email directly or view it on GitHub.

@jreback
Copy link
Contributor

jreback commented Jan 3, 2015

for implementation see here: https://github.com/pydata/pandas/blob/master/pandas/tseries/common.py#L55

just create a subclass of PandasDelegate and this is quite easy

@sinhrks
Copy link
Member

sinhrks commented Jan 17, 2015

Yeah, having both should be better. kind is for programming and plot accessor is for interactive use.

I think reason of current plotting complexity is also caused by some options which are rarely used. Let me list up as separate issue.

@shoyer shoyer changed the title API: Add dedicated plot methods like plot_scatter or plot.scatter instead of using kind? API: dedicated plot methods like plot.scatter as an alternative to using kind Jan 21, 2015
shoyer added a commit to shoyer/pandas that referenced this issue Jan 21, 2015
Fixes pandas-dev#9124

This PR adds plotting sub-methods like `df.plot.scatter()` as an alternative
to using `df.plot(kind='scatter')`.

I've added meaningful function signatures and documentation for a few of these
methods, but I would greatly appreciate help to fill in the rest -- this is a
lot of documentation to write! The entire point of this PR, of course, is to
have better introspection and docstrings.

Todo list:

- [ ] Function signatures
	- [ ] `area`
	- [ ] `line`
	- [ ] `bar`
	- [ ] `barh`
	- [x] `box`
	- [x] `hexbin`
	- [x] `hist`
	- [ ] `kde`/`density`
	- [ ] `pie`
	- [x] `scatter`
- [ ] Docstrings
	- [ ] `area`
	- [ ] `line`
	- [ ] `bar`
	- [ ] `barh`
	- [ ] `box`
	- [ ] `hexbin`
	- [x] `hist`
	- [ ] `kde`/`density`
	- [ ] `pie`
	- [ ] `scatter`
- [ ] Tests (just a few)
- [ ] Plotting docs
- [ ] API docs
- [ ] Release notes
@shoyer shoyer mentioned this issue Jan 21, 2015
16 tasks
@shoyer
Copy link
Member Author

shoyer commented Jan 21, 2015

I have a preliminary PR up at #9321. This should be a reasonable framework (I've verified that introspection seems to work properly with IPython), but I could definitely use help assembling appropriate function signatures and docstrings :).

@jreback jreback added this to the 0.17.0 milestone Sep 6, 2015
shoyer added a commit to shoyer/pandas that referenced this issue Sep 10, 2015
Fixes pandas-dev#9124

This PR adds plotting sub-methods like `df.plot.scatter()` as an alternative
to using `df.plot(kind='scatter')`.

I've added meaningful function signatures and documentation for a few of these
methods, but I would greatly appreciate help to fill in the rest -- this is a
lot of documentation to write! The entire point of this PR, of course, is to
have better introspection and docstrings.
nickeubank pushed a commit to nickeubank/pandas that referenced this issue Sep 29, 2015
Fixes pandas-dev#9124

This PR adds plotting sub-methods like `df.plot.scatter()` as an alternative
to using `df.plot(kind='scatter')`.

I've added meaningful function signatures and documentation for a few of these
methods, but I would greatly appreciate help to fill in the rest -- this is a
lot of documentation to write! The entire point of this PR, of course, is to
have better introspection and docstrings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants