Skip to content
This repository

Add Phil Elson's percentage histogram example #1149

Merged
merged 2 commits into from over 1 year ago

3 participants

Damon McDougall Phil Elson Michael Droettboom
Damon McDougall
Collaborator

Addresses issue #320. The example is @pelson's, I just added some comments. Also, Gaussians look pretty.

Phil Elson
Collaborator

Seems fine. Did you look into the potential of using a scale? I've never done it that way, but it seems that (in my head at least) that would be a viable solution too.

I'm happy enough with this approach though. +1

Damon McDougall
Collaborator

I did not. I thought yours was good enough! :)

Michael Droettboom
Owner

It might be slightly better if the formatter added a percent symbol at the end, but otherwise, I think this is good to go.

Milestoning this to 1.2.x since #320 is also.

Damon McDougall Add a percent symbol to tick labels
Got to be careful when latex is involved.
1985162
Damon McDougall
Collaborator

So yeah, it's a bit more fiddly to use the percent symbol with latex.

Michael Droettboom mdboom merged commit 47a61d7 into from August 28, 2012
Michael Droettboom mdboom closed this August 28, 2012
Michael Droettboom mdboom referenced this pull request August 28, 2012
Closed

hist plot in percent #320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Aug 27, 2012
Damon McDougall Add Phil Elson's percentage histogram example 0c22d3b
Aug 28, 2012
Damon McDougall Add a percent symbol to tick labels
Got to be careful when latex is involved.
1985162
This page is out of date. Refresh to see the latest.
29  examples/pylab_examples/histogram_percent_demo.py
... ...
@@ -0,0 +1,29 @@
  1
+import matplotlib
  2
+from numpy.random import randn
  3
+import matplotlib.pyplot as plt
  4
+from matplotlib.ticker import FuncFormatter
  5
+
  6
+def to_percent(y, position):
  7
+    # Ignore the passed in position. This has the effect of scaling the default
  8
+    # tick locations.
  9
+    s = str(100 * y)
  10
+
  11
+    # The percent symbol needs escaping in latex
  12
+    if matplotlib.rcParams['text.usetex'] == True:
  13
+        return s + r'$\%$'
  14
+    else:
  15
+        return s + '%'
  16
+
  17
+x = randn(5000)
  18
+
  19
+# Make a normed histogram. It'll be multiplied by 100 later.
  20
+plt.hist(x, bins=50, normed=True)
  21
+
  22
+# Create the formatter using the function to_percent. This multiplies all the
  23
+# default labels by 100, making them all percentages
  24
+formatter = FuncFormatter(to_percent)
  25
+
  26
+# Set the formatter
  27
+plt.gca().yaxis.set_major_formatter(formatter)
  28
+
  29
+plt.show()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.