-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add a common example to compare style sheets #6476
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
NelleV
merged 13 commits into
matplotlib:master
from
afvincent:common_example_for_style_sheets
Oct 21, 2016
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2934740
Add a common example to compare style sheets
9eec6d6
PEP8 fixes and add labels in 1st subplot
a56734f
Fix remaining PEP8 issues
671f467
rely on rcParams['axes.prop_cycle'] to iterate over the colors
50a516e
Docstrings corrections/Code clarification/Add a 2nd scatter cloud
afvincent d9da90c
Fix typos, use more clever annotation position and use tight_layout
afvincent edaa9e8
Fix the docstrings to be more PEP257-compliant
afvincent c56bcdd
Switch to a single row of demo plots
afvincent 5bf87dd
Cosmeticks tweaks
afvincent 828bec2
Switch script docstring to sphinx-gallery style
afvincent 42bbad5
Add 'default' and group styles ~ in lexicographic order
afvincent 25e1e74
Fix default value of the named parameter in
afvincent 09e1f3a
Fix a typo in the example docstring
afvincent 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,146 @@ | ||
""" | ||
====================== | ||
Style sheets reference | ||
====================== | ||
|
||
This script demonstrates the different available style sheets on a | ||
common set of example plots: scatter plot, image, bar graph, patches, | ||
line plot and histogram, | ||
|
||
""" | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def plot_scatter(ax, prng, nb_samples=100): | ||
"""Scatter plot. | ||
""" | ||
for mu, sigma, marker in [(-.5, 0.75, 'o'), (0.75, 1., 's')]: | ||
x, y = prng.normal(loc=mu, scale=sigma, size=(2, nb_samples)) | ||
ax.plot(x, y, ls='none', marker=marker) | ||
ax.set_xlabel('X-label') | ||
return ax | ||
|
||
|
||
def plot_colored_sinusoidal_lines(ax): | ||
"""Plot sinusoidal lines with colors following the style color cycle. | ||
""" | ||
L = 2 * np.pi | ||
x = np.linspace(0, L) | ||
nb_colors = len(plt.rcParams['axes.prop_cycle']) | ||
shift = np.linspace(0, L, nb_colors, endpoint=False) | ||
for s in shift: | ||
ax.plot(x, np.sin(x + s), '-') | ||
ax.set_xlim([x[0], x[-1]]) | ||
return ax | ||
|
||
|
||
def plot_bar_graphs(ax, prng, min_value=5, max_value=25, nb_samples=5): | ||
"""Plot two bar graphs side by side, with letters as x-tick labels. | ||
""" | ||
x = np.arange(nb_samples) | ||
ya, yb = prng.randint(min_value, max_value, size=(2, nb_samples)) | ||
width = 0.25 | ||
ax.bar(x, ya, width) | ||
ax.bar(x + width, yb, width, color='C2') | ||
ax.set_xticks(x + width) | ||
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) | ||
return ax | ||
|
||
|
||
def plot_colored_circles(ax, prng, nb_samples=15): | ||
"""Plot circle patches. | ||
|
||
NB: draws a fixed amount of samples, rather than using the length of | ||
the color cycle, because different styles may have different numbers | ||
of colors. | ||
""" | ||
for sty_dict, j in zip(plt.rcParams['axes.prop_cycle'], range(nb_samples)): | ||
ax.add_patch(plt.Circle(prng.normal(scale=3, size=2), | ||
radius=1.0, color=sty_dict['color'])) | ||
# Force the limits to be the same across the styles (because different | ||
# styles may have different numbers of available colors). | ||
ax.set_xlim([-4, 8]) | ||
ax.set_ylim([-5, 6]) | ||
ax.set_aspect('equal', adjustable='box') # to plot circles as circles | ||
return ax | ||
|
||
|
||
def plot_image_and_patch(ax, prng, size=(20, 20)): | ||
"""Plot an image with random values and superimpose a circular patch. | ||
""" | ||
values = prng.random_sample(size=size) | ||
ax.imshow(values, interpolation='none') | ||
c = plt.Circle((5, 5), radius=5, label='patch') | ||
ax.add_patch(c) | ||
# Remove ticks | ||
ax.set_xticks([]) | ||
ax.set_yticks([]) | ||
|
||
|
||
def plot_histograms(ax, prng, nb_samples=10000): | ||
"""Plot 4 histograms and a text annotation. | ||
""" | ||
params = ((10, 10), (4, 12), (50, 12), (6, 55)) | ||
for a, b in params: | ||
values = prng.beta(a, b, size=nb_samples) | ||
ax.hist(values, histtype="stepfilled", bins=30, alpha=0.8, normed=True) | ||
# Add a small annotation. | ||
ax.annotate('Annotation', xy=(0.25, 4.25), xycoords='data', | ||
xytext=(0.9, 0.9), textcoords='axes fraction', | ||
va="top", ha="right", | ||
bbox=dict(boxstyle="round", alpha=0.2), | ||
arrowprops=dict( | ||
arrowstyle="->", | ||
connectionstyle="angle,angleA=-95,angleB=35,rad=10"), | ||
) | ||
return ax | ||
|
||
|
||
def plot_figure(style_label=""): | ||
"""Setup and plot the demonstration figure with a given style. | ||
""" | ||
# Use a dedicated RandomState instance to draw the same "random" values | ||
# across the different figures. | ||
prng = np.random.RandomState(96917002) | ||
|
||
# Tweak the figure size to be better suited for a row of numerous plots: | ||
# double the width and halve the height. NB: use relative changes because | ||
# some styles may have a figure size different from the default one. | ||
(fig_width, fig_height) = plt.rcParams['figure.figsize'] | ||
fig_size = [fig_width * 2, fig_height / 2] | ||
|
||
fig, axes = plt.subplots(ncols=6, nrows=1, num=style_label, | ||
figsize=fig_size, squeeze=True) | ||
axes[0].set_ylabel(style_label) | ||
|
||
plot_scatter(axes[0], prng) | ||
plot_image_and_patch(axes[1], prng) | ||
plot_bar_graphs(axes[2], prng) | ||
plot_colored_circles(axes[3], prng) | ||
plot_colored_sinusoidal_lines(axes[4]) | ||
plot_histograms(axes[5], prng) | ||
|
||
fig.tight_layout() | ||
|
||
return fig | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# Setup a list of all available styles, in alphabetical order but | ||
# the `default` and `classic` ones, which will be forced resp. in | ||
# first and second position. | ||
style_list = list(plt.style.available) # *new* list: avoids side effects. | ||
style_list.remove('classic') # `classic` is in the list: first remove it. | ||
style_list.sort() | ||
style_list.insert(0, u'default') | ||
style_list.insert(1, u'classic') | ||
|
||
# Plot a demonstration figure for every available style sheet. | ||
for style_label in style_list: | ||
with plt.style.context(style_label): | ||
fig = plot_figure(style_label=style_label) | ||
|
||
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.
Wouldn't “Plot sinusoidal lines with colors following the style color cycle.” be more correct?