Section 108.1: Seaborn

Seaborn 是 Matplotlib 的封装程序,可轻松创建常用的统计图。支持的绘图列表包括单变量和双变量分布图、回归图以及一些用于绘制分类变量的方法。Seaborn 提供的全部绘图列表见其 API 参考。

在 Seaborn 中创建图形非常简单,只需调用相应的绘图函数即可。下面是为随机生成的数据创建直方图、核密度估计和崎岖图的示例。

import numpy as np # numpy used to create data from plotting
import seaborn as sns # common form of importing seaborn

# Generate normally distributed data
data = np.random.randn(1000)

# Plot a histogram with both a rugplot and kde graph superimposed
sns.distplot(data, kde=True, rug=True)

Seaborn

还可以使用声明式语法来控制绘制图表的风格。

# Using previously created imports and data.

# Use a dark background with no grid.
sns.set_style('dark')

# Create the plot again
sns.distplot(data, kde=True, rug=True)

Seaborn

额外的好处是,普通的matplotlib命令仍然可以应用于Seaborn绘图。下面是在前面创建的直方图中添加坐标轴标题的示例。

# Using previously created data and style

# Access to matplotlib commands
import matplotlib.pyplot as plt

# Previously created plot.
sns.distplot(data, kde=True, rug=True)
# Set the axis labels.
plt.xlabel('This is my x-axis')
plt.ylabel('This is my y-axis')

Seaborn

Section 108.2: Matplotlib

Matplotlib 是一个用于 Python 的数学绘图库,提供各种不同的绘图功能。

Matplotlib 文档可在此处找到,SO 文档可在此处找到。

Matplotlib 提供了两种不同的绘图方法,不过它们在大多数情况下是可以互换的:

  • 首先,matplotlib 提供了 pyplot 接口,这是一个直接且简单易用的接口,允许以类似 MATLAB 的风格绘制复杂图形。

  • 其次,matplotlib 允许用户使用基于对象的系统直接控制不同方面(坐标轴、线条、刻度线等)。这比较困难,但可以完全控制整个图形。

下面是一个使用 pyplot 界面绘制一些生成数据的示例:

import matplotlib.pyplot as plt

# Generate some data for plotting.
x = [0, 1, 2, 3, 4, 5, 6]
y = [i**2 for i in x]

# Plot the data x, y with some keyword arguments that control the plot style.
# Use two different plot commands to plot both points (scatter) and a line (plot).
plt.scatter(x, y, c='blue', marker='x', s=100) # Create blue markers of shape "x" and size 100
plt.plot(x, y, color='red', linewidth=2) # Create a red line with linewidth 2.

# Add some text to the axes and a title.
plt.xlabel('x data')
plt.ylabel('y data')
plt.title('An example plot')

# Generate the plot and show to the user.
plt.show()

Matplotlib

请注意,由于在交互模式下运行 matplotlib.pyplot 时,plt.show() 在某些环境下可能会出现问题,如果出现这种情况,可以通过传递一个可选参数 plt.show(block=True) 来显式地覆盖阻塞行为,以缓解问题。

Section 108.3: Plotly

Plotly 是一个现代绘图和数据可视化平台。Plotly 可作为 Python、R、JavaScript、Julia 和 MATLAB 的库,用于制作各种图表,尤其是数据科学图表。它还可以作为网络应用程序与这些语言一起使用。

用户可以安装 plotly 库,并在用户认证后离线使用。此处给出了该库的安装和离线验证方法。此外,还可以在 Jupyter Notebooks 中绘制地图。

使用该库需要一个带有用户名和密码的账户。这样就有了在云端保存绘图和数据的工作空间。

该库的免费版功能略有限制,每天只能绘制 250 个图块。付费版具有所有功能,可无限下载地块,并提供更多私人数据存储空间。更多详情,请点击此处访问主页。

文档和示例请点击此处

import plotly.graph_objs as go
import plotly as ply

# Create random data with numpy
import numpy as np

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5

# Create traces
trace0 = go.Scatter(
  x = random_x,
  y = random_y0,
  mode = 'lines',
  name = 'lines'
)
trace1 = go.Scatter(
  x = random_x,
  y = random_y1,
  mode = 'lines+markers',
  name = 'lines+markers'
)
trace2 = go.Scatter(
  x = random_x,
  y = random_y2,
  mode = 'markers',
  name = 'markers'
)
data = [trace0, trace1, trace2]

ply.offline.plot(data, filename='line-mode')

Plotly

Section 108.4: MayaVI

MayaVI 是用于科学数据的三维可视化工具。它在引擎盖下使用可视化工具包或 VTK。利用 VTK 的强大功能,MayaVI 能够生成各种三维绘图和图形。它既可以作为一个单独的软件应用程序,也可以作为一个库。与 Matplotlib 类似,该库提供了一个面向对象的编程语言接口,无需了解 VTK 即可创建绘图。

MayaVI 仅在 Python 2.7x 系列中可用!希望很快能在 Python 3-x 系列中使用!(尽管在 Python 3 中使用其依赖项取得了一些成功)。

文档可在此处找到。一些图库示例可在此处找到

下面是文档中使用 MayaVI 绘制的示例图。

from numpy import sin, cos, mgrid, pi, sqrt
from mayavi import mlab

mlab.figure(fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))
u, v = mgrid[- 0.035:pi:0.01, - 0.035:pi:0.01]

X = 2 / 3. * (cos(u) * cos(2 * v) + sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
Y = 2 / 3. * (cos(u) * sin(2 * v) - sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
Z = -sqrt(2) * cos(u) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
S = sin(u)
mlab.mesh(X, Y, Z, scalars=S, colormap='YlGnBu', )

# Nice view from the front
mlab.view(.0, - 5.0, 4)
mlab.show()

MayaVI