Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bce844

Browse files
authored
Merge pull request#6476 from afvincent/common_example_for_style_sheets
Add a common example to compare style sheets
2 parentsc93dff4 +09e1f3a commit7bce844

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"""
2+
======================
3+
Style sheets reference
4+
======================
5+
6+
This script demonstrates the different available style sheets on a
7+
common set of example plots: scatter plot, image, bar graph, patches,
8+
line plot and histogram,
9+
10+
"""
11+
12+
importnumpyasnp
13+
importmatplotlib.pyplotasplt
14+
15+
16+
defplot_scatter(ax,prng,nb_samples=100):
17+
"""Scatter plot.
18+
"""
19+
formu,sigma,markerin [(-.5,0.75,'o'), (0.75,1.,'s')]:
20+
x,y=prng.normal(loc=mu,scale=sigma,size=(2,nb_samples))
21+
ax.plot(x,y,ls='none',marker=marker)
22+
ax.set_xlabel('X-label')
23+
returnax
24+
25+
26+
defplot_colored_sinusoidal_lines(ax):
27+
"""Plot sinusoidal lines with colors following the style color cycle.
28+
"""
29+
L=2*np.pi
30+
x=np.linspace(0,L)
31+
nb_colors=len(plt.rcParams['axes.prop_cycle'])
32+
shift=np.linspace(0,L,nb_colors,endpoint=False)
33+
forsinshift:
34+
ax.plot(x,np.sin(x+s),'-')
35+
ax.set_xlim([x[0],x[-1]])
36+
returnax
37+
38+
39+
defplot_bar_graphs(ax,prng,min_value=5,max_value=25,nb_samples=5):
40+
"""Plot two bar graphs side by side, with letters as x-tick labels.
41+
"""
42+
x=np.arange(nb_samples)
43+
ya,yb=prng.randint(min_value,max_value,size=(2,nb_samples))
44+
width=0.25
45+
ax.bar(x,ya,width)
46+
ax.bar(x+width,yb,width,color='C2')
47+
ax.set_xticks(x+width)
48+
ax.set_xticklabels(['a','b','c','d','e'])
49+
returnax
50+
51+
52+
defplot_colored_circles(ax,prng,nb_samples=15):
53+
"""Plot circle patches.
54+
55+
NB: draws a fixed amount of samples, rather than using the length of
56+
the color cycle, because different styles may have different numbers
57+
of colors.
58+
"""
59+
forsty_dict,jinzip(plt.rcParams['axes.prop_cycle'],range(nb_samples)):
60+
ax.add_patch(plt.Circle(prng.normal(scale=3,size=2),
61+
radius=1.0,color=sty_dict['color']))
62+
# Force the limits to be the same across the styles (because different
63+
# styles may have different numbers of available colors).
64+
ax.set_xlim([-4,8])
65+
ax.set_ylim([-5,6])
66+
ax.set_aspect('equal',adjustable='box')# to plot circles as circles
67+
returnax
68+
69+
70+
defplot_image_and_patch(ax,prng,size=(20,20)):
71+
"""Plot an image with random values and superimpose a circular patch.
72+
"""
73+
values=prng.random_sample(size=size)
74+
ax.imshow(values,interpolation='none')
75+
c=plt.Circle((5,5),radius=5,label='patch')
76+
ax.add_patch(c)
77+
# Remove ticks
78+
ax.set_xticks([])
79+
ax.set_yticks([])
80+
81+
82+
defplot_histograms(ax,prng,nb_samples=10000):
83+
"""Plot 4 histograms and a text annotation.
84+
"""
85+
params= ((10,10), (4,12), (50,12), (6,55))
86+
fora,binparams:
87+
values=prng.beta(a,b,size=nb_samples)
88+
ax.hist(values,histtype="stepfilled",bins=30,alpha=0.8,normed=True)
89+
# Add a small annotation.
90+
ax.annotate('Annotation',xy=(0.25,4.25),xycoords='data',
91+
xytext=(0.9,0.9),textcoords='axes fraction',
92+
va="top",ha="right",
93+
bbox=dict(boxstyle="round",alpha=0.2),
94+
arrowprops=dict(
95+
arrowstyle="->",
96+
connectionstyle="angle,angleA=-95,angleB=35,rad=10"),
97+
)
98+
returnax
99+
100+
101+
defplot_figure(style_label=""):
102+
"""Setup and plot the demonstration figure with a given style.
103+
"""
104+
# Use a dedicated RandomState instance to draw the same "random" values
105+
# across the different figures.
106+
prng=np.random.RandomState(96917002)
107+
108+
# Tweak the figure size to be better suited for a row of numerous plots:
109+
# double the width and halve the height. NB: use relative changes because
110+
# some styles may have a figure size different from the default one.
111+
(fig_width,fig_height)=plt.rcParams['figure.figsize']
112+
fig_size= [fig_width*2,fig_height/2]
113+
114+
fig,axes=plt.subplots(ncols=6,nrows=1,num=style_label,
115+
figsize=fig_size,squeeze=True)
116+
axes[0].set_ylabel(style_label)
117+
118+
plot_scatter(axes[0],prng)
119+
plot_image_and_patch(axes[1],prng)
120+
plot_bar_graphs(axes[2],prng)
121+
plot_colored_circles(axes[3],prng)
122+
plot_colored_sinusoidal_lines(axes[4])
123+
plot_histograms(axes[5],prng)
124+
125+
fig.tight_layout()
126+
127+
returnfig
128+
129+
130+
if__name__=="__main__":
131+
132+
# Setup a list of all available styles, in alphabetical order but
133+
# the `default` and `classic` ones, which will be forced resp. in
134+
# first and second position.
135+
style_list=list(plt.style.available)# *new* list: avoids side effects.
136+
style_list.remove('classic')# `classic` is in the list: first remove it.
137+
style_list.sort()
138+
style_list.insert(0,u'default')
139+
style_list.insert(1,u'classic')
140+
141+
# Plot a demonstration figure for every available style sheet.
142+
forstyle_labelinstyle_list:
143+
withplt.style.context(style_label):
144+
fig=plot_figure(style_label=style_label)
145+
146+
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp