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,37 @@
# 轴方向演示步骤01
![轴方向演示步骤01示例](https://matplotlib.org/_images/sphx_glr_axis_direction_demo_step01_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_axes(ax)
ax.set_ylim(-0.1, 1.5)
ax.set_yticks([0, 1])
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(1, 0.5)
ax.axis["x"].set_axisline_style("->", size=1.5)
return ax
fig = plt.figure(figsize=(3, 2.5))
fig.subplots_adjust(top=0.8)
ax1 = setup_axes(fig, "111")
ax1.axis["x"].set_axis_direction("left")
plt.show()
```
## 下载这个示例
- [下载python源码: axis_direction_demo_step01.py](https://matplotlib.org/_downloads/axis_direction_demo_step01.py)
- [下载Jupyter notebook: axis_direction_demo_step01.ipynb](https://matplotlib.org/_downloads/axis_direction_demo_step01.ipynb)

View File

@@ -0,0 +1,48 @@
# 轴方向演示步骤02
![轴方向演示步骤02示例](https://matplotlib.org/_images/sphx_glr_axis_direction_demo_step02_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_axes(ax)
ax.set_ylim(-0.1, 1.5)
ax.set_yticks([0, 1])
#ax.axis[:].toggle(all=False)
#ax.axis[:].line.set_visible(False)
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(1, 0.5)
ax.axis["x"].set_axisline_style("->", size=1.5)
return ax
fig = plt.figure(figsize=(6, 2.5))
fig.subplots_adjust(bottom=0.2, top=0.8)
ax1 = setup_axes(fig, "121")
ax1.axis["x"].set_ticklabel_direction("+")
ax1.annotate("ticklabel direction=$+$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
ax2 = setup_axes(fig, "122")
ax2.axis["x"].set_ticklabel_direction("-")
ax2.annotate("ticklabel direction=$-$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
plt.show()
```
## 下载这个示例
- [下载python源码: axis_direction_demo_step02.py](https://matplotlib.org/_downloads/axis_direction_demo_step02.py)
- [下载Jupyter notebook: axis_direction_demo_step02.ipynb](https://matplotlib.org/_downloads/axis_direction_demo_step02.ipynb)

View File

@@ -0,0 +1,52 @@
# 轴方向演示步骤03
![轴方向演示步骤03示例](https://matplotlib.org/_images/sphx_glr_axis_direction_demo_step03_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_axes(ax)
ax.set_ylim(-0.1, 1.5)
ax.set_yticks([0, 1])
#ax.axis[:].toggle(all=False)
#ax.axis[:].line.set_visible(False)
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(1, 0.5)
ax.axis["x"].set_axisline_style("->", size=1.5)
return ax
fig = plt.figure(figsize=(6, 2.5))
fig.subplots_adjust(bottom=0.2, top=0.8)
ax1 = setup_axes(fig, "121")
ax1.axis["x"].label.set_text("Label")
ax1.axis["x"].toggle(ticklabels=False)
ax1.axis["x"].set_axislabel_direction("+")
ax1.annotate("label direction=$+$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
ax2 = setup_axes(fig, "122")
ax2.axis["x"].label.set_text("Label")
ax2.axis["x"].toggle(ticklabels=False)
ax2.axis["x"].set_axislabel_direction("-")
ax2.annotate("label direction=$-$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
plt.show()
```
## 下载这个示例
- [下载python源码: axis_direction_demo_step03.py](https://matplotlib.org/_downloads/axis_direction_demo_step03.py)
- [下载Jupyter notebook: axis_direction_demo_step03.ipynb](https://matplotlib.org/_downloads/axis_direction_demo_step03.ipynb)

View File

@@ -0,0 +1,65 @@
# 轴方向演示步骤04
![轴方向演示步骤04示例](https://matplotlib.org/_images/sphx_glr_axis_direction_demo_step04_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_axes(ax)
ax.set_ylim(-0.1, 1.5)
ax.set_yticks([0, 1])
ax.axis[:].set_visible(False)
ax.axis["x1"] = ax.new_floating_axis(1, 0.3)
ax.axis["x1"].set_axisline_style("->", size=1.5)
ax.axis["x2"] = ax.new_floating_axis(1, 0.7)
ax.axis["x2"].set_axisline_style("->", size=1.5)
return ax
fig = plt.figure(figsize=(6, 2.5))
fig.subplots_adjust(bottom=0.2, top=0.8)
ax1 = setup_axes(fig, "121")
ax1.axis["x1"].label.set_text("rotation=0")
ax1.axis["x1"].toggle(ticklabels=False)
ax1.axis["x2"].label.set_text("rotation=10")
ax1.axis["x2"].label.set_rotation(10)
ax1.axis["x2"].toggle(ticklabels=False)
ax1.annotate("label direction=$+$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
ax2 = setup_axes(fig, "122")
ax2.axis["x1"].set_axislabel_direction("-")
ax2.axis["x2"].set_axislabel_direction("-")
ax2.axis["x1"].label.set_text("rotation=0")
ax2.axis["x1"].toggle(ticklabels=False)
ax2.axis["x2"].label.set_text("rotation=10")
ax2.axis["x2"].label.set_rotation(10)
ax2.axis["x2"].toggle(ticklabels=False)
ax2.annotate("label direction=$-$", (0.5, 0), xycoords="axes fraction",
xytext=(0, -10), textcoords="offset points",
va="top", ha="center")
plt.show()
```
## 下载这个示例
- [下载python源码: axis_direction_demo_step04.py](https://matplotlib.org/_downloads/axis_direction_demo_step04.py)
- [下载Jupyter notebook: axis_direction_demo_step04.ipynb](https://matplotlib.org/_downloads/axis_direction_demo_step04.ipynb)

View File

@@ -0,0 +1,101 @@
# 演示轴方向
![演示轴方向示例](https://matplotlib.org/_images/sphx_glr_demo_axis_direction_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.angle_helper as angle_helper
import mpl_toolkits.axisartist.grid_finder as grid_finder
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist as axisartist
from mpl_toolkits.axisartist.grid_helper_curvelinear import \
GridHelperCurveLinear
def setup_axes(fig, rect):
"""
polar projection, but in a rectangular box.
"""
# see demo_curvelinear_grid.py for details
tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
extreme_finder = angle_helper.ExtremeFinderCycle(20, 20,
lon_cycle=360,
lat_cycle=None,
lon_minmax=None,
lat_minmax=(0, np.inf),
)
grid_locator1 = angle_helper.LocatorDMS(12)
grid_locator2 = grid_finder.MaxNLocator(5)
tick_formatter1 = angle_helper.FormatterDMS()
grid_helper = GridHelperCurveLinear(tr,
extreme_finder=extreme_finder,
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1
)
ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
ax1.axis[:].toggle(ticklabels=False)
fig.add_subplot(ax1)
ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
return ax1
def add_floating_axis1(ax1):
ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30)
axis.label.set_text(r"$\theta = 30^{\circ}$")
axis.label.set_visible(True)
return axis
def add_floating_axis2(ax1):
ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6)
axis.label.set_text(r"$r = 6$")
axis.label.set_visible(True)
return axis
fig = plt.figure(1, figsize=(8, 4))
fig.clf()
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99,
wspace=0.01, hspace=0.01)
for i, d in enumerate(["bottom", "left", "top", "right"]):
ax1 = setup_axes(fig, rect=241++i)
axis = add_floating_axis1(ax1)
axis.set_axis_direction(d)
ax1.annotate(d, (0, 1), (5, -5),
xycoords="axes fraction", textcoords="offset points",
va="top", ha="left")
for i, d in enumerate(["bottom", "left", "top", "right"]):
ax1 = setup_axes(fig, rect=245++i)
axis = add_floating_axis2(ax1)
axis.set_axis_direction(d)
ax1.annotate(d, (0, 1), (5, -5),
xycoords="axes fraction", textcoords="offset points",
va="top", ha="left")
plt.show()
```
## 下载这个示例
- [下载python源码: demo_axis_direction.py](https://matplotlib.org/_downloads/demo_axis_direction.py)
- [下载Jupyter notebook: demo_axis_direction.ipynb](https://matplotlib.org/_downloads/demo_axis_direction.ipynb)

View File

@@ -0,0 +1,37 @@
# 轴线样式
此示例显示了轴样式的一些配置。
![轴线样式示例](https://matplotlib.org/_images/sphx_glr_demo_axisline_style_001.png)
```python
from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
if 1:
fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
ax.axis[direction].set_axisline_style("-|>")
# adds X and Y-axis from the origin
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
# hides borders
ax.axis[direction].set_visible(False)
x = np.linspace(-0.5, 1., 100)
ax.plot(x, np.sin(x*np.pi))
plt.show()
```
## 下载这个示例
- [下载python源码: demo_axisline_style.py](https://matplotlib.org/_downloads/demo_axisline_style.py)
- [下载Jupyter notebook: demo_axisline_style.ipynb](https://matplotlib.org/_downloads/demo_axisline_style.ipynb)

View File

@@ -0,0 +1,142 @@
# 演示Curvelinear网格
自定义网格和记号行。
此示例演示如何通过在网格上应用转换使用GridHelperCurve线性来定义自定义网格和注释行。这可以用作第二个打印上的演示用于在矩形框中创建极轴投影。
![Curvelinear网格示例](https://matplotlib.org/_images/sphx_glr_demo_curvelinear_grid_0011.png)
```python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from mpl_toolkits.axisartist import Subplot
from mpl_toolkits.axisartist import SubplotHost, \
ParasiteAxesAuxTrans
from mpl_toolkits.axisartist.grid_helper_curvelinear import \
GridHelperCurveLinear
def curvelinear_test1(fig):
"""
grid for custom transform.
"""
def tr(x, y):
x, y = np.asarray(x), np.asarray(y)
return x, y - x
def inv_tr(x, y):
x, y = np.asarray(x), np.asarray(y)
return x, y + x
grid_helper = GridHelperCurveLinear((tr, inv_tr))
ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)
# ax1 will have a ticks and gridlines defined by the given
# transform (+ transData of the Axes). Note that the transform of
# the Axes itself (i.e., transData) is not affected by the given
# transform.
fig.add_subplot(ax1)
xx, yy = tr([3, 6], [5.0, 10.])
ax1.plot(xx, yy, linewidth=2.0)
ax1.set_aspect(1.)
ax1.set_xlim(0, 10.)
ax1.set_ylim(0, 10.)
ax1.axis["t"] = ax1.new_floating_axis(0, 3.)
ax1.axis["t2"] = ax1.new_floating_axis(1, 7.)
ax1.grid(True, zorder=0)
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
def curvelinear_test2(fig):
"""
polar projection, but in a rectangular box.
"""
# PolarAxes.PolarTransform takes radian. However, we want our coordinate
# system in degree
tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
# polar projection, which involves cycle, and also has limits in
# its coordinates, needs a special method to find the extremes
# (min, max of the coordinate within the view).
# 20, 20 : number of sampling points along x, y direction
extreme_finder = angle_helper.ExtremeFinderCycle(20, 20,
lon_cycle=360,
lat_cycle=None,
lon_minmax=None,
lat_minmax=(0, np.inf),
)
grid_locator1 = angle_helper.LocatorDMS(12)
# Find a grid values appropriate for the coordinate (degree,
# minute, second).
tick_formatter1 = angle_helper.FormatterDMS()
# And also uses an appropriate formatter. Note that,the
# acceptable Locator and Formatter class is a bit different than
# that of mpl's, and you cannot directly use mpl's Locator and
# Formatter here (but may be possible in the future).
grid_helper = GridHelperCurveLinear(tr,
extreme_finder=extreme_finder,
grid_locator1=grid_locator1,
tick_formatter1=tick_formatter1
)
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
# make ticklabels of right and top axis visible.
ax1.axis["right"].major_ticklabels.set_visible(True)
ax1.axis["top"].major_ticklabels.set_visible(True)
# let right axis shows ticklabels for 1st coordinate (angle)
ax1.axis["right"].get_helper().nth_coord_ticks = 0
# let bottom axis shows ticklabels for 2nd coordinate (radius)
ax1.axis["bottom"].get_helper().nth_coord_ticks = 1
fig.add_subplot(ax1)
# A parasite axes with given transform
ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal")
# note that ax2.transData == tr + ax1.transData
# Anything you draw in ax2 will match the ticks and grids of ax1.
ax1.parasites.append(ax2)
intp = cbook.simple_linear_interpolation
ax2.plot(intp(np.array([0, 30]), 50),
intp(np.array([10., 10.]), 50),
linewidth=2.0)
ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
ax1.grid(True, zorder=0)
if 1:
fig = plt.figure(1, figsize=(7, 4))
fig.clf()
curvelinear_test1(fig)
curvelinear_test2(fig)
plt.show()
```
## 下载这个示例
- [下载python源码: demo_curvelinear_grid.py](https://matplotlib.org/_downloads/demo_curvelinear_grid.py)
- [下载Jupyter notebook: demo_curvelinear_grid.ipynb](https://matplotlib.org/_downloads/demo_curvelinear_grid.ipynb)

View File

@@ -0,0 +1,75 @@
# 演示Curvelinear网格2
自定义网格和记号行。
此示例演示如何通过在网格上应用转换使用GridHelperCurve线性来定义自定义网格和注释行。作为打印上的演示轴上将显示5x5矩阵。
![Curvelinear网格2示例](https://matplotlib.org/_images/sphx_glr_demo_curvelinear_grid2_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.grid_helper_curvelinear import \
GridHelperCurveLinear
from mpl_toolkits.axisartist.axislines import Subplot
import mpl_toolkits.axisartist.angle_helper as angle_helper
def curvelinear_test1(fig):
"""
grid for custom transform.
"""
def tr(x, y):
sgn = np.sign(x)
x, y = np.abs(np.asarray(x)), np.asarray(y)
return sgn*x**.5, y
def inv_tr(x, y):
sgn = np.sign(x)
x, y = np.asarray(x), np.asarray(y)
return sgn*x**2, y
extreme_finder = angle_helper.ExtremeFinderCycle(20, 20,
lon_cycle=None,
lat_cycle=None,
# (0, np.inf),
lon_minmax=None,
lat_minmax=None,
)
grid_helper = GridHelperCurveLinear((tr, inv_tr),
extreme_finder=extreme_finder)
ax1 = Subplot(fig, 111, grid_helper=grid_helper)
# ax1 will have a ticks and gridlines defined by the given
# transform (+ transData of the Axes). Note that the transform of
# the Axes itself (i.e., transData) is not affected by the given
# transform.
fig.add_subplot(ax1)
ax1.imshow(np.arange(25).reshape(5, 5),
vmax=50, cmap=plt.cm.gray_r,
interpolation="nearest",
origin="lower")
# tick density
grid_helper.grid_finder.grid_locator1._nbins = 6
grid_helper.grid_finder.grid_locator2._nbins = 6
if 1:
fig = plt.figure(1, figsize=(7, 4))
fig.clf()
curvelinear_test1(fig)
plt.show()
```
## 下载这个示例
- [下载python源码: demo_curvelinear_grid2.py](https://matplotlib.org/_downloads/demo_curvelinear_grid2.py)
- [下载Jupyter notebook: demo_curvelinear_grid2.ipynb](https://matplotlib.org/_downloads/demo_curvelinear_grid2.ipynb)

View File

@@ -0,0 +1,160 @@
# 演示浮动轴
浮动轴的演示。
```python
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import numpy as np
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import (FixedLocator, MaxNLocator,
DictFormatter)
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
def setup_axes1(fig, rect):
"""
A simple one.
"""
tr = Affine2D().scale(2, 1).rotate_deg(30)
grid_helper = floating_axes.GridHelperCurveLinear(
tr, extremes=(-0.5, 3.5, 0, 4))
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
aux_ax = ax1.get_aux_axes(tr)
grid_helper.grid_finder.grid_locator1._nbins = 4
grid_helper.grid_finder.grid_locator2._nbins = 4
return ax1, aux_ax
def setup_axes2(fig, rect):
"""
With custom locator and formatter.
Note that the extreme values are swapped.
"""
tr = PolarAxes.PolarTransform()
pi = np.pi
angle_ticks = [(0, r"$0$"),
(.25*pi, r"$\frac{1}{4}\pi$"),
(.5*pi, r"$\frac{1}{2}\pi$")]
grid_locator1 = FixedLocator([v for v, s in angle_ticks])
tick_formatter1 = DictFormatter(dict(angle_ticks))
grid_locator2 = MaxNLocator(2)
grid_helper = floating_axes.GridHelperCurveLinear(
tr, extremes=(.5*pi, 0, 2, 1),
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1,
tick_formatter2=None)
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
# create a parasite axes whose transData in RA, cz
aux_ax = ax1.get_aux_axes(tr)
aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax
ax1.patch.zorder = 0.9 # but this has a side effect that the patch is
# drawn twice, and possibly over some other
# artists. So, we decrease the zorder a bit to
# prevent this.
return ax1, aux_ax
def setup_axes3(fig, rect):
"""
Sometimes, things like axis_direction need to be adjusted.
"""
# rotate a bit for better orientation
tr_rotate = Affine2D().translate(-95, 0)
# scale degree to radians
tr_scale = Affine2D().scale(np.pi/180., 1.)
tr = tr_rotate + tr_scale + PolarAxes.PolarTransform()
grid_locator1 = angle_helper.LocatorHMS(4)
tick_formatter1 = angle_helper.FormatterHMS()
grid_locator2 = MaxNLocator(3)
# Specify theta limits in degrees
ra0, ra1 = 8.*15, 14.*15
# Specify radial limits
cz0, cz1 = 0, 14000
grid_helper = floating_axes.GridHelperCurveLinear(
tr, extremes=(ra0, ra1, cz0, cz1),
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1,
tick_formatter2=None)
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
# adjust axis
ax1.axis["left"].set_axis_direction("bottom")
ax1.axis["right"].set_axis_direction("top")
ax1.axis["bottom"].set_visible(False)
ax1.axis["top"].set_axis_direction("bottom")
ax1.axis["top"].toggle(ticklabels=True, label=True)
ax1.axis["top"].major_ticklabels.set_axis_direction("top")
ax1.axis["top"].label.set_axis_direction("top")
ax1.axis["left"].label.set_text(r"cz [km$^{-1}$]")
ax1.axis["top"].label.set_text(r"$\alpha_{1950}$")
# create a parasite axes whose transData in RA, cz
aux_ax = ax1.get_aux_axes(tr)
aux_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax
ax1.patch.zorder = 0.9 # but this has a side effect that the patch is
# drawn twice, and possibly over some other
# artists. So, we decrease the zorder a bit to
# prevent this.
return ax1, aux_ax
```
```python
fig = plt.figure(1, figsize=(8, 4))
fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)
ax1, aux_ax1 = setup_axes1(fig, 131)
aux_ax1.bar([0, 1, 2, 3], [3, 2, 1, 3])
ax2, aux_ax2 = setup_axes2(fig, 132)
theta = np.random.rand(10)*.5*np.pi
radius = np.random.rand(10) + 1.
aux_ax2.scatter(theta, radius)
ax3, aux_ax3 = setup_axes3(fig, 133)
theta = (8 + np.random.rand(10)*(14 - 8))*15. # in degrees
radius = np.random.rand(10)*14000.
aux_ax3.scatter(theta, radius)
plt.show()
```
![演示浮动轴](https://matplotlib.org/_images/sphx_glr_demo_floating_axes_001.png)
## 下载这个示例
- [下载python源码: demo_floating_axes.py](https://matplotlib.org/_downloads/demo_floating_axes.py)
- [下载Jupyter notebook: demo_floating_axes.ipynb](https://matplotlib.org/_downloads/demo_floating_axes.ipynb)

View File

@@ -0,0 +1,76 @@
# 演示浮动轴2
轴在矩形框内
以下代码演示了如何将浮动极坐标曲线放在矩形框内。 为了更好地了解极坐标曲线请查看demo_curvelinear_grid.py。
![演示浮动轴2](https://matplotlib.org/_images/sphx_glr_demo_floating_axis_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
from mpl_toolkits.axisartist import SubplotHost
from mpl_toolkits.axisartist import GridHelperCurveLinear
def curvelinear_test2(fig):
"""Polar projection, but in a rectangular box.
"""
# see demo_curvelinear_grid.py for details
tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform()
extreme_finder = angle_helper.ExtremeFinderCycle(20,
20,
lon_cycle=360,
lat_cycle=None,
lon_minmax=None,
lat_minmax=(0,
np.inf),
)
grid_locator1 = angle_helper.LocatorDMS(12)
tick_formatter1 = angle_helper.FormatterDMS()
grid_helper = GridHelperCurveLinear(tr,
extreme_finder=extreme_finder,
grid_locator1=grid_locator1,
tick_formatter1=tick_formatter1
)
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
fig.add_subplot(ax1)
# Now creates floating axis
# floating axis whose first coordinate (theta) is fixed at 60
ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60)
axis.label.set_text(r"$\theta = 60^{\circ}$")
axis.label.set_visible(True)
# floating axis whose second coordinate (r) is fixed at 6
ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6)
axis.label.set_text(r"$r = 6$")
ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
ax1.grid(True)
fig = plt.figure(1, figsize=(5, 5))
fig.clf()
curvelinear_test2(fig)
plt.show()
```
## 下载这个示例
- [下载python源码: demo_floating_axis.py](https://matplotlib.org/_downloads/demo_floating_axis.py)
- [下载Jupyter notebook: demo_floating_axis.ipynb](https://matplotlib.org/_downloads/demo_floating_axis.ipynb)

View File

@@ -0,0 +1,65 @@
# 演示寄生轴
创建寄生轴。这些轴将与主体轴共享x比例但在y方向显示不同的比例。
请注意,此方法使用 [parasite_axes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.html#module-mpl_toolkits.axes_grid1.parasite_axes) 的 [HostAxes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.HostAxes.html#mpl_toolkits.axes_grid1.parasite_axes.HostAxes) 和 [ParasiteAxes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes.html#mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes)。 在 [Demo Parasite Axes2](https://matplotlib.org/api/toolkits/axes_grid1.html#toolkit-axesgrid1-index) 示例中可以找到使用[Matplotlib axes_grid1 Toolkit](https://matplotlib.org/api/toolkits/axisartist.html#toolkit-axisartist-index) 和 Matplotlib axisartist Toolkit的替代方法。 使用通常的matplotlib子图的替代方法显示在 [Multiple Yaxis With Spines](https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html) 示例中。
![演示寄生轴](https://matplotlib.org/_images/sphx_glr_demo_parasite_axes_001.png)
```python
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt
fig = plt.figure(1)
host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
par1 = ParasiteAxes(host, sharex=host)
par2 = ParasiteAxes(host, sharex=host)
host.parasites.append(par1)
host.parasites.append(par2)
host.set_ylabel("Density")
host.set_xlabel("Distance")
host.axis["right"].set_visible(False)
par1.axis["right"].set_visible(True)
par1.set_ylabel("Temperature")
par1.axis["right"].major_ticklabels.set_visible(True)
par1.axis["right"].label.set_visible(True)
par2.set_ylabel("Velocity")
offset = (60, 0)
new_axisline = par2._grid_helper.new_fixed_axis
par2.axis["right2"] = new_axisline(loc="right", axes=par2, offset=offset)
fig.add_axes(host)
host.set_xlim(0, 2)
host.set_ylim(0, 2)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right2"].label.set_color(p3.get_color())
plt.show()
```
## 下载这个示例
- [下载python源码: demo_parasite_axes.py](https://matplotlib.org/_downloads/demo_parasite_axes.py)
- [下载Jupyter notebook: demo_parasite_axes.ipynb](https://matplotlib.org/_downloads/demo_parasite_axes.ipynb)

View File

@@ -0,0 +1,58 @@
# 演示寄生轴2
寄生轴的演示。
以下代码是寄生虫轴的示例。 它旨在展示如何在一个单独的图上绘制多个不同的值。 请注意在此示例中par1和par2都调用twinx这意味着两者都直接绑定到x轴。 从那里这两个轴中的每一个可以彼此分开地表现这意味着它们可以从它们自身以及x轴上获取单独的值。
请注意,此方法使用 [parasite_axes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.html#module-mpl_toolkits.axes_grid1.parasite_axes) 的 [HostAxes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.HostAxes.html#mpl_toolkits.axes_grid1.parasite_axes.HostAxes) 和 [ParasiteAxes](https://matplotlib.org/api/_as_gen/mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes.html#mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes)。 在 [Demo Parasite Axes2](https://matplotlib.org/api/toolkits/axes_grid1.html#toolkit-axesgrid1-index) 示例中可以找到使用[Matplotlib axes_grid1 Toolkit](https://matplotlib.org/api/toolkits/axisartist.html#toolkit-axisartist-index) 和 Matplotlib axisartist Toolkit的替代方法。 使用通常的matplotlib子图的替代方法显示在 [Multiple Yaxis With Spines](https://matplotlib.org/gallery/ticks_and_spines/multiple_yaxis_with_spines.html) 示例中。
![演示寄生轴2](https://matplotlib.org/_images/sphx_glr_demo_parasite_axes2_001.png)
```python
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)
host.set_xlim(0, 2)
host.set_ylim(0, 2)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())
plt.show()
```
## 下载这个示例
- [下载python源码: demo_parasite_axes2.py](https://matplotlib.org/_downloads/demo_parasite_axes2.py)
- [下载Jupyter notebook: demo_parasite_axes2.ipynb](https://matplotlib.org/_downloads/demo_parasite_axes2.ipynb)

View File

@@ -0,0 +1,47 @@
# Ticklabel对齐演示
![Ticklabel对齐演示](https://matplotlib.org/_images/sphx_glr_demo_ticklabel_alignment_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_subplot(ax)
ax.set_yticks([0.2, 0.8])
ax.set_yticklabels(["short", "loooong"])
ax.set_xticks([0.2, 0.8])
ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"])
return ax
fig = plt.figure(1, figsize=(3, 5))
fig.subplots_adjust(left=0.5, hspace=0.7)
ax = setup_axes(fig, 311)
ax.set_ylabel("ha=right")
ax.set_xlabel("va=baseline")
ax = setup_axes(fig, 312)
ax.axis["left"].major_ticklabels.set_ha("center")
ax.axis["bottom"].major_ticklabels.set_va("top")
ax.set_ylabel("ha=center")
ax.set_xlabel("va=top")
ax = setup_axes(fig, 313)
ax.axis["left"].major_ticklabels.set_ha("left")
ax.axis["bottom"].major_ticklabels.set_va("bottom")
ax.set_ylabel("ha=left")
ax.set_xlabel("va=bottom")
plt.show()
```
## 下载这个示例
- [下载python源码: demo_ticklabel_alignment.py](https://matplotlib.org/_downloads/demo_ticklabel_alignment.py)
- [下载Jupyter notebook: demo_ticklabel_alignment.ipynb](https://matplotlib.org/_downloads/demo_ticklabel_alignment.ipynb)

View File

@@ -0,0 +1,47 @@
# Ticklabel方向演示
![Ticklabel方向演示](https://matplotlib.org/_images/sphx_glr_demo_ticklabel_alignment_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_subplot(ax)
ax.set_yticks([0.2, 0.8])
ax.set_yticklabels(["short", "loooong"])
ax.set_xticks([0.2, 0.8])
ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"])
return ax
fig = plt.figure(1, figsize=(3, 5))
fig.subplots_adjust(left=0.5, hspace=0.7)
ax = setup_axes(fig, 311)
ax.set_ylabel("ha=right")
ax.set_xlabel("va=baseline")
ax = setup_axes(fig, 312)
ax.axis["left"].major_ticklabels.set_ha("center")
ax.axis["bottom"].major_ticklabels.set_va("top")
ax.set_ylabel("ha=center")
ax.set_xlabel("va=top")
ax = setup_axes(fig, 313)
ax.axis["left"].major_ticklabels.set_ha("left")
ax.axis["bottom"].major_ticklabels.set_va("bottom")
ax.set_ylabel("ha=left")
ax.set_xlabel("va=bottom")
plt.show()
```
## 下载这个示例
- [下载python源码: demo_ticklabel_alignment.py](https://matplotlib.org/_downloads/demo_ticklabel_alignment.py)
- [下载Jupyter notebook: demo_ticklabel_alignment.ipynb](https://matplotlib.org/_downloads/demo_ticklabel_alignment.ipynb)

View File

@@ -0,0 +1,26 @@
# 简单轴方向01
![简单轴方向01](https://matplotlib.org/_images/sphx_glr_simple_axis_direction01_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
fig = plt.figure(figsize=(4, 2.5))
ax1 = fig.add_subplot(axisartist.Subplot(fig, "111"))
fig.subplots_adjust(right=0.8)
ax1.axis["left"].major_ticklabels.set_axis_direction("top")
ax1.axis["left"].label.set_text("Label")
ax1.axis["right"].label.set_visible(True)
ax1.axis["right"].label.set_text("Label")
ax1.axis["right"].label.set_axis_direction("left")
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axis_direction01.py](https://matplotlib.org/_downloads/simple_axis_direction01.py)
- [下载Jupyter notebook: simple_axis_direction01.ipynb](https://matplotlib.org/_downloads/simple_axis_direction01.ipynb)

View File

@@ -0,0 +1,41 @@
# 简单轴方向03
![简单轴方向03](https://matplotlib.org/_images/sphx_glr_simple_axis_direction03_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_subplot(ax)
ax.set_yticks([0.2, 0.8])
ax.set_xticks([0.2, 0.8])
return ax
fig = plt.figure(1, figsize=(5, 2))
fig.subplots_adjust(wspace=0.4, bottom=0.3)
ax1 = setup_axes(fig, "121")
ax1.set_xlabel("X-label")
ax1.set_ylabel("Y-label")
ax1.axis[:].invert_ticklabel_direction()
ax2 = setup_axes(fig, "122")
ax2.set_xlabel("X-label")
ax2.set_ylabel("Y-label")
ax2.axis[:].major_ticks.set_tick_out(True)
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axis_direction03.py](https://matplotlib.org/_downloads/simple_axis_direction03.py)
- [下载Jupyter notebook: simple_axis_direction03.ipynb](https://matplotlib.org/_downloads/simple_axis_direction03.ipynb)

View File

@@ -0,0 +1,114 @@
# 简单的轴垫
![简单的轴垫](https://matplotlib.org/_images/sphx_glr_simple_axis_pad_001.png)
```python
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.angle_helper as angle_helper
import mpl_toolkits.axisartist.grid_finder as grid_finder
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist as axisartist
from mpl_toolkits.axisartist.grid_helper_curvelinear import \
GridHelperCurveLinear
def setup_axes(fig, rect):
"""
polar projection, but in a rectangular box.
"""
# see demo_curvelinear_grid.py for details
tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
extreme_finder = angle_helper.ExtremeFinderCycle(20, 20,
lon_cycle=360,
lat_cycle=None,
lon_minmax=None,
lat_minmax=(0, np.inf),
)
grid_locator1 = angle_helper.LocatorDMS(12)
grid_locator2 = grid_finder.MaxNLocator(5)
tick_formatter1 = angle_helper.FormatterDMS()
grid_helper = GridHelperCurveLinear(tr,
extreme_finder=extreme_finder,
grid_locator1=grid_locator1,
grid_locator2=grid_locator2,
tick_formatter1=tick_formatter1
)
ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
ax1.axis[:].set_visible(False)
fig.add_subplot(ax1)
ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
return ax1
def add_floating_axis1(ax1):
ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30)
axis.label.set_text(r"$\theta = 30^{\circ}$")
axis.label.set_visible(True)
return axis
def add_floating_axis2(ax1):
ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6)
axis.label.set_text(r"$r = 6$")
axis.label.set_visible(True)
return axis
fig = plt.figure(1, figsize=(9, 3.))
fig.clf()
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99,
wspace=0.01, hspace=0.01)
def ann(ax1, d):
if plt.rcParams["text.usetex"]:
d = d.replace("_", r"\_")
ax1.annotate(d, (0.5, 1), (5, -5),
xycoords="axes fraction", textcoords="offset points",
va="top", ha="center")
ax1 = setup_axes(fig, rect=141)
axis = add_floating_axis1(ax1)
ann(ax1, r"default")
ax1 = setup_axes(fig, rect=142)
axis = add_floating_axis1(ax1)
axis.major_ticklabels.set_pad(10)
ann(ax1, r"ticklabels.set_pad(10)")
ax1 = setup_axes(fig, rect=143)
axis = add_floating_axis1(ax1)
axis.label.set_pad(20)
ann(ax1, r"label.set_pad(20)")
ax1 = setup_axes(fig, rect=144)
axis = add_floating_axis1(ax1)
axis.major_ticks.set_tick_out(True)
ann(ax1, "ticks.set_tick_out(True)")
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axis_pad.py](https://matplotlib.org/_downloads/simple_axis_pad.py)
- [下载Jupyter notebook: simple_axis_pad.ipynb](https://matplotlib.org/_downloads/simple_axis_pad.ipynb)

View File

@@ -0,0 +1,32 @@
# 简单的Axisartist1
![简单的Axisartist1](https://matplotlib.org/_images/sphx_glr_simple_axisartist1_001.png)
```python
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as AA
fig = plt.figure(1)
fig.subplots_adjust(right=0.85)
ax = AA.Subplot(fig, 1, 1, 1)
fig.add_subplot(ax)
# make some axis invisible
ax.axis["bottom", "top", "right"].set_visible(False)
# make an new axis along the first axis axis (x-axis) which pass
# through y=0.
ax.axis["y=0"] = ax.new_floating_axis(nth_coord=0, value=0,
axis_direction="bottom")
ax.axis["y=0"].toggle(all=True)
ax.axis["y=0"].label.set_text("y = 0")
ax.set_ylim(-2, 4)
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axisartist1.py](https://matplotlib.org/_downloads/simple_axisartist1.py)
- [下载Jupyter notebook: simple_axisartist1.ipynb](https://matplotlib.org/_downloads/simple_axisartist1.ipynb)

View File

@@ -0,0 +1,45 @@
# 简单的轴线
![简单的轴线](https://matplotlib.org/_images/sphx_glr_simple_axisline_001.png)
```python
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
fig = plt.figure(1)
fig.subplots_adjust(right=0.85)
ax = SubplotZero(fig, 1, 1, 1)
fig.add_subplot(ax)
# make right and top axis invisible
ax.axis["right"].set_visible(False)
ax.axis["top"].set_visible(False)
# make xzero axis (horizontal axis line through y=0) visible.
ax.axis["xzero"].set_visible(True)
ax.axis["xzero"].label.set_text("Axis Zero")
ax.set_ylim(-2, 4)
ax.set_xlabel("Label X")
ax.set_ylabel("Label Y")
# or
#ax.axis["bottom"].label.set_text("Label X")
#ax.axis["left"].label.set_text("Label Y")
# make new (right-side) yaxis, but with some offset
offset = (20, 0)
new_axisline = ax.get_grid_helper().new_fixed_axis
ax.axis["right2"] = new_axisline(loc="right", offset=offset, axes=ax)
ax.axis["right2"].label.set_text("Label Y2")
ax.plot([-2, 3, 2])
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axisline.py](https://matplotlib.org/_downloads/simple_axisline.py)
- [下载Jupyter notebook: simple_axisline.ipynb](https://matplotlib.org/_downloads/simple_axisline.ipynb)

View File

@@ -0,0 +1,34 @@
# 简单的轴线2
![简单的轴线2](https://matplotlib.org/_images/sphx_glr_simple_axisline2_001.png)
```python
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
import numpy as np
fig = plt.figure(1, (4, 3))
# a subplot with two additional axis, "xzero" and "yzero". "xzero" is
# y=0 line, and "yzero" is x=0 line.
ax = SubplotZero(fig, 1, 1, 1)
fig.add_subplot(ax)
# make xzero axis (horizontal axis line through y=0) visible.
ax.axis["xzero"].set_visible(True)
ax.axis["xzero"].label.set_text("Axis Zero")
# make other axis (bottom, top, right) invisible.
for n in ["bottom", "top", "right"]:
ax.axis[n].set_visible(False)
xx = np.arange(0, 2*np.pi, 0.01)
ax.plot(xx, np.sin(xx))
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axisline2.py](https://matplotlib.org/_downloads/simple_axisline2.py)
- [下载Jupyter notebook: simple_axisline2.ipynb](https://matplotlib.org/_downloads/simple_axisline2.ipynb)

View File

@@ -0,0 +1,23 @@
# 简单的轴线3
![简单的轴线3](https://matplotlib.org/_images/sphx_glr_simple_axisline3_001.png)
```python
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import Subplot
fig = plt.figure(1, (3, 3))
ax = Subplot(fig, 111)
fig.add_subplot(ax)
ax.axis["right"].set_visible(False)
ax.axis["top"].set_visible(False)
plt.show()
```
## 下载这个示例
- [下载python源码: simple_axisline3.py](https://matplotlib.org/_downloads/simple_axisline3.py)
- [下载Jupyter notebook: simple_axisline3.ipynb](https://matplotlib.org/_downloads/simple_axisline3.ipynb)