-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
nested pie example #8546
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
nested pie example #8546
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """ | ||
| ================= | ||
| Nested pie charts | ||
| ================= | ||
|
|
||
| The following examples show two ways to build a nested pie chart | ||
| in Matplotlib. | ||
|
|
||
| """ | ||
|
|
||
| from matplotlib import pyplot as plt | ||
| import numpy as np | ||
|
|
||
| ############################################################################### | ||
| # The most straightforward way to build a pie chart is to use the | ||
| # :meth:`pie method <matplotlib.axes.Axes.pie>` | ||
| # | ||
| # In this case, pie takes values corresponding to counts in a group. | ||
| # We'll first generate some fake data, corresponding to three groups. | ||
| # In the outer circle, we'll treat each number as belonging to its | ||
| # own group. In the inner circle, we'll plot them as members of their | ||
| # original 3 groups. | ||
|
|
||
| vals = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]) | ||
| fig, ax = plt.subplots() | ||
| ax.pie(vals.flatten(), radius=1.2, | ||
| colors=plt.rcParams["axes.prop_cycle"].by_key()["color"][:vals.shape[1]]) | ||
| ax.pie(vals.sum(axis=1), radius=1) | ||
| ax.set(aspect="equal", title='Pie plot with `ax.pie`') | ||
|
|
||
| plt.show() | ||
|
|
||
| ############################################################################### | ||
| # However, you can accomplish the same output by using a bar plot on | ||
| # axes with a polar coordinate system. This may give more flexibility on | ||
| # the exact design of the plot. | ||
| # | ||
| # In this case, we need to map x-values of the bar chart onto radians of | ||
| # a circle. | ||
|
|
||
| fig, ax = plt.subplots(subplot_kw=dict(polar=True)) | ||
|
|
||
| left_inner = np.arange(0.0, 2 * np.pi, 2 * np.pi / 6) | ||
| left_middle = np.arange(0.0, 2 * np.pi, 2 * np.pi / 12) | ||
| left_outer = np.arange(0.0, 2 * np.pi, 2 * np.pi / 9) | ||
|
|
||
| ax.bar(left=left_inner, | ||
| width=2 * np.pi / 6, bottom=0, color='C0', | ||
| linewidth=2, edgecolor='w', | ||
| height=np.zeros_like(left_inner) + 5) | ||
|
|
||
| ax.bar(left=left_middle, | ||
| width=2 * np.pi / 12, bottom=5, color='C1', | ||
| linewidth=2, edgecolor='w', | ||
| height=np.zeros_like(left_middle) + 2) | ||
|
|
||
| ax.bar(left=left_outer, | ||
| width=2 * np.pi / 9, bottom=7, color='C2', | ||
| linewidth=2, edgecolor='w', | ||
| height=np.zeros_like(left_outer) + 3) | ||
|
|
||
| ax.set(title="Pie plot with `ax.bar` and polar coordinates") | ||
| ax.set_axis_off() | ||
| plt.show() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the two examples should show the same data (if not necessarily the same style). For the one below, that probably means calling something like
np.histogram(data, cumulative=True) * 2 * np.piand fiddling with partial sums.