mirror of
https://github.com/Estom/notes.git
synced 2026-04-04 03:17:44 +08:00
matplotlib & pandas
This commit is contained in:
35
Python/matplotlab/gallery/style_sheets/bmh.md
Normal file
35
Python/matplotlab/gallery/style_sheets/bmh.md
Normal 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/
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
32
Python/matplotlab/gallery/style_sheets/dark_background.md
Normal file
32
Python/matplotlab/gallery/style_sheets/dark_background.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# 黑色的背景样式表
|
||||
|
||||
此示例演示了 “dark_background” 样式,该样式使用白色表示通常为黑色的元素(文本,边框等)。请注意,并非所有绘图元素都默认为由rc参数定义的颜色。
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
35
Python/matplotlab/gallery/style_sheets/fivethirtyeight.md
Normal file
35
Python/matplotlab/gallery/style_sheets/fivethirtyeight.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# FiveThirtyEight样式表
|
||||
|
||||
这显示了“fivethirtyeight”样式的一个示例,它试图从FiveThirtyEight.com复制样式。
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
59
Python/matplotlab/gallery/style_sheets/ggplot.md
Normal file
59
Python/matplotlab/gallery/style_sheets/ggplot.md
Normal 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/
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
44
Python/matplotlab/gallery/style_sheets/grayscale.md
Normal file
44
Python/matplotlab/gallery/style_sheets/grayscale.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# 灰度样式表
|
||||
|
||||
此示例演示“灰度”样式表,该样式表将定义为rc参数的所有颜色更改为灰度。 但请注意,并非所有绘图元素都默认为rc参数定义的颜色。
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
@@ -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
|
||||
- 应用布局规则
|
||||
|
||||

|
||||
|
||||
```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)
|
||||
203
Python/matplotlab/gallery/style_sheets/style_sheets_reference.md
Normal file
203
Python/matplotlab/gallery/style_sheets/style_sheets_reference.md
Normal file
@@ -0,0 +1,203 @@
|
||||
# 样式表参考
|
||||
|
||||
此脚本演示了一组常见示例图上的不同可用样式表:散点图,图像,条形图,面片,线图和直方图,
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
```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)
|
||||
Reference in New Issue
Block a user