matplotlib & pandas

This commit is contained in:
estomm
2020-09-26 22:03:11 +08:00
parent 73cc328c81
commit d31be4f219
599 changed files with 99925 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
# 黑客贝叶斯方法样式表
这个例子演示了贝叶斯黑客方法 [[1]](https://matplotlib.org/gallery/style_sheets/bmh.html#id2) 在线书籍中使用的风格。
[[1]](https://matplotlib.org/gallery/style_sheets/bmh.html#id1) http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
![黑客贝叶斯方法样式表示例](https://matplotlib.org/_images/sphx_glr_bmh_001.png)
```python
from numpy.random import beta
import matplotlib.pyplot as plt
plt.style.use('bmh')
def plot_beta_hist(ax, a, b):
ax.hist(beta(a, b, size=10000), histtype="stepfilled",
bins=25, alpha=0.8, density=True)
fig, ax = plt.subplots()
plot_beta_hist(ax, 10, 10)
plot_beta_hist(ax, 4, 12)
plot_beta_hist(ax, 50, 12)
plot_beta_hist(ax, 6, 55)
ax.set_title("'bmh' style sheet")
plt.show()
```
## 下载这个示例
- [下载python源码: bmh.py](https://matplotlib.org/_downloads/bmh.py)
- [下载Jupyter notebook: bmh.ipynb](https://matplotlib.org/_downloads/bmh.ipynb)

View File

@@ -0,0 +1,32 @@
# 黑色的背景样式表
此示例演示了 “dark_background” 样式该样式使用白色表示通常为黑色的元素文本边框等。请注意并非所有绘图元素都默认为由rc参数定义的颜色。
![黑色的背景样式表示例](https://matplotlib.org/_images/sphx_glr_dark_background_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background')
fig, ax = plt.subplots()
L = 6
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.prop_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
ax.plot(x, np.sin(x + s), 'o-')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_title("'dark_background' style sheet")
plt.show()
```
## 下载这个示例
- [下载python源码: dark_background.py](https://matplotlib.org/_downloads/dark_background.py)
- [下载Jupyter notebook: dark_background.ipynb](https://matplotlib.org/_downloads/dark_background.ipynb)

View File

@@ -0,0 +1,35 @@
# FiveThirtyEight样式表
这显示了“fivethirtyeight”样式的一个示例它试图从FiveThirtyEight.com复制样式。
![FiveThirtyEight样式表示例](https://matplotlib.org/_images/sphx_glr_fivethirtyeight_001.png)
```python
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('fivethirtyeight')
x = np.linspace(0, 10)
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x) + x + np.random.randn(50))
ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50))
ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50))
ax.plot(x, np.sin(x) + np.random.randn(50))
ax.set_title("'fivethirtyeight' style sheet")
plt.show()
```
## 下载这个示例
- [下载python源码: fivethirtyeight.py](https://matplotlib.org/_downloads/fivethirtyeight.py)
- [下载Jupyter notebook: fivethirtyeight.ipynb](https://matplotlib.org/_downloads/fivethirtyeight.ipynb)

View File

@@ -0,0 +1,59 @@
# ggplot样式表
此示例演示了“ggplot”样式该样式调整样式以模拟[ggplot](http://ggplot2.org/)[R](https://www.r-project.org/)的流行绘图包)。
这些设置被无耻地从[[1]](https://matplotlib.org/gallery/style_sheets/ggplot.html#id2)窃取(经允许)。
[[1]](https://matplotlib.org/gallery/style_sheets/ggplot.html#id1) https://web.archive.org/web/20111215111010/http://www.huyng.com/archives/sane-color-scheme-for-matplotlib/691/
![ggplot样式表示例](https://matplotlib.org/_images/sphx_glr_ggplot_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, axes = plt.subplots(ncols=2, nrows=2)
ax1, ax2, ax3, ax4 = axes.ravel()
# scatter plot (Note: `plt.scatter` doesn't use default colors)
x, y = np.random.normal(size=(2, 200))
ax1.plot(x, y, 'o')
# sinusoidal lines with colors from default color cycle
L = 2*np.pi
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.prop_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
ax2.plot(x, np.sin(x + s), '-')
ax2.margins(0)
# bar graphs
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax3.bar(x, y1, width)
ax3.bar(x + width, y2, width,
color=list(plt.rcParams['axes.prop_cycle'])[2]['color'])
ax3.set_xticks(x + width)
ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
# circles with colors from default color cycle
for i, color in enumerate(plt.rcParams['axes.prop_cycle']):
xy = np.random.normal(size=2)
ax4.add_patch(plt.Circle(xy, radius=0.3, color=color['color']))
ax4.axis('equal')
ax4.margins(0)
plt.show()
```
## 下载这个示例
- [下载python源码: ggplot.py](https://matplotlib.org/_downloads/ggplot.py)
- [下载Jupyter notebook: ggplot.ipynb](https://matplotlib.org/_downloads/ggplot.ipynb)

View File

@@ -0,0 +1,44 @@
# 灰度样式表
此示例演示“灰度”样式表该样式表将定义为rc参数的所有颜色更改为灰度。 但请注意并非所有绘图元素都默认为rc参数定义的颜色。
![灰度样式表示例](https://matplotlib.org/_images/sphx_glr_grayscale_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
def color_cycle_example(ax):
L = 6
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.prop_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
ax.plot(x, np.sin(x + s), 'o-')
def image_and_patch_example(ax):
ax.imshow(np.random.random(size=(20, 20)), interpolation='none')
c = plt.Circle((5, 5), radius=5, label='patch')
ax.add_patch(c)
plt.style.use('grayscale')
fig, (ax1, ax2) = plt.subplots(ncols=2)
fig.suptitle("'grayscale' style sheet")
color_cycle_example(ax1)
image_and_patch_example(ax2)
plt.show()
```
## 下载这个示例
- [下载python源码: grayscale.py](https://matplotlib.org/_downloads/grayscale.py)
- [下载Jupyter notebook: grayscale.ipynb](https://matplotlib.org/_downloads/grayscale.ipynb)

View File

@@ -0,0 +1,44 @@
# Solarized Light样式表
这显示了一个“Solarized_Light”样式的示例它试图复制以下样式
- http://ethanschoonover.com/solarized
- https://github.com/jrnold/ggthemes
- http://pygal.org/en/stable/documentation/builtin_styles.html#light-solarized
并且:
使用调色板的所有8个重音 - 从蓝色开始
进行:
- 为条形图和堆积图创建Alpha值。 .33或.5
- 应用布局规则
![Solarized Light样式表示例](https://matplotlib.org/_images/sphx_glr_plot_solarizedlight2_001.png)
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10)
with plt.style.context('Solarize_Light2'):
plt.plot(x, np.sin(x) + x + np.random.randn(50))
plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 4 + np.random.randn(50))
plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 6 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 7 * x + np.random.randn(50))
plt.plot(x, np.sin(x) + 8 * x + np.random.randn(50))
# Number of accent colors in the color scheme
plt.title('8 Random Lines - Line')
plt.xlabel('x label', fontsize=14)
plt.ylabel('y label', fontsize=14)
plt.show()
```
## 下载这个示例
- [下载python源码: plot_solarizedlight2.py](https://matplotlib.org/_downloads/plot_solarizedlight2.py)
- [下载Jupyter notebook: plot_solarizedlight2.ipynb](https://matplotlib.org/_downloads/plot_solarizedlight2.ipynb)

View File

@@ -0,0 +1,203 @@
# 样式表参考
此脚本演示了一组常见示例图上的不同可用样式表:散点图,图像,条形图,面片,线图和直方图,
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_001.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_002.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_003.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_004.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_005.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_006.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_007.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_008.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_009.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_010.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_011.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_012.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_013.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_014.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_015.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_016.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_017.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_018.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_019.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_020.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_021.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_022.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_023.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_024.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_025.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_026.png)
![样式表参考示例](https://matplotlib.org/_images/sphx_glr_style_sheets_reference_027.png)
```python
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
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, density=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 = ['default', 'classic'] + sorted(
style for style in plt.style.available if style != '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()
```
脚本的总运行时间0分5.216秒)
## 下载这个示例
- [下载python源码: style_sheets_reference.py](https://matplotlib.org/_downloads/style_sheets_reference.py)
- [下载Jupyter notebook: style_sheets_reference.ipynb](https://matplotlib.org/_downloads/style_sheets_reference.ipynb)