Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Doc: Scatter Hist example update #13775

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

Conversation

ImportanceOfBeingErnest
Copy link
Member

@ImportanceOfBeingErnest ImportanceOfBeingErnest commented Mar 28, 2019

PR Summary

Update the Scatter Hist examples. Partially inspired by @jklymak's comment on stackoverflow.

  • Show alternative methods using add_axes as well as gridspec. (Previously plt.axes was used.)
  • Interlink the two examples from the main lib and the axes_grid1 toolkit.
  • Add explanations.

Previously:

With this PR:

PR Checklist

  • Code is Flake 8 compliant
  • Examples if plot related
  • Documentation is sphinx and numpydoc compliant

@anntzer
Copy link
Contributor

anntzer commented Mar 28, 2019

Perhaps also

diff --git i/examples/axes_grid1/scatter_hist_locatable_axes.py w/examples/axes_grid1/scatter_hist_locatable_axes.py
index 5786a382e..3611ddb1d 100644
--- i/examples/axes_grid1/scatter_hist_locatable_axes.py
+++ w/examples/axes_grid1/scatter_hist_locatable_axes.py
@@ -24,11 +24,13 @@ axScatter.scatter(x, y)
 axScatter.set_aspect(1.)
 
 # create new axes on the right and on the top of the current axes
-# The first argument of the new_vertical(new_horizontal) method is
-# the height (width) of the axes to be created in inches.
 divider = make_axes_locatable(axScatter)
-axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
-axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
+axHistx = divider.append_axes(
+    "top", size=1.2, pad=0.1,  # Both sizes are in inches.
+    sharex=axScatter)
+axHisty = divider.append_axes(
+    "right", size=1.2, pad=0.1,
+    sharey=axScatter)
 
 # make some labels invisible
 axHistx.xaxis.set_tick_params(labelbottom=False)


"""

#############################################################################
# Let us first define a function that takes x and y data as input, as well
Copy link
Member

Choose a reason for hiding this comment

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

There will be no space in the generated ReST code between the docstring (stripped) and the documentation comment. Here, the two texts get merged into one paragraph.

Not sure if you can do this at all or if you have to continue the docstring. Anyway this should have a section title.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is actually desired - in the sense of, I do not want a new section here (because the two sections are the two examples mentionned earlier). I now put everything in the heading docstring.

divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
axHistx = divider.append_axes("top", 1.2, pad=0.1, # height and pad in inches
Copy link
Member

Choose a reason for hiding this comment

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

Would the plots look better with pad=0?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think they look bad in the first place. With pad=0 the axis of the main plot would directly continue into the independent axis of the marginal plot; hence one would need to introduce another visual distinction.

@ImportanceOfBeingErnest ImportanceOfBeingErnest force-pushed the doc-scatter-hist-example branch 2 times, most recently from a9ee73e to 5261a1e Compare August 7, 2019 19:27
x = np.random.randn(1000)
y = np.random.randn(1000)


def scatter_hist(x, y, axScatter, axHistx, axHisty):
Copy link
Member

@timhoffm timhoffm Aug 7, 2019

Choose a reason for hiding this comment

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

I find the logical cut a bit peculiar. A function plotting data into a scatter and two hist plots into three axes is quite special; in particular since the label locations and orientations of the hist plots imply additional assumptions on the axes layout.

This is not a function you would write in real-world applications; instead one would write a function generating the whole figure. This particular function is created to not duplicate the code of filling the axes. I feel uncomfortable having such a function in examples.

This makes me think: What is the purpose of the example:

  1. Demonstrate this particular case of plot.
  2. Demonstrate how complex layouts of multiple axes can be created in different ways.

Originally the first. IMHO adding the second makes the example more complicated to understand. It's probably better to do this in a separate example (maybe even without data) and only refer is here ("See ... for different ways of achieving the axes layout").

Copy link
Member Author

@ImportanceOfBeingErnest ImportanceOfBeingErnest Aug 7, 2019

Choose a reason for hiding this comment

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

I write such functions all the time, because it allows me to have the figure layout and the actual plotting separated. E.g. one could have the axes be part of an even bigger layout without changing anything within the function.

Showcasing the two ways of achieving this plot allows users to choose which one suits their needs. Initially we only had the first case of manually positionning the axes. But then users would ask how to calculate the coordinates of the rectangles in case this whole bunch is to be positionned in a 2x2 axes layout.

A function plotting data into a scatter and two hist plots into three axes is quite special

Seaborn has this as its own function called jointplot, and also provides a general purpose JointGrid to map dataframes into such 3 axes. In R/ggplot it's called ggMarginal, in Origin it's "Marginal-Histograms-Graph".

Copy link
Contributor

Choose a reason for hiding this comment

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

unrelated: pep8-ify the variable names? (ax_histx, etc.)

@tacaswell tacaswell merged commit 90f6a35 into matplotlib:master Sep 1, 2019
@tacaswell
Copy link
Member

Thank @ImportanceOfBeingErnest and @timhoffm !

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