Plotly 简明教程
Plotly - 3D Scatter and Surface Plot
本章将提供有关三维(3D)散点图和 3D 曲面图的信息,以及如何借助 Plotly 制作它们。
This chapter will give information about the three-dimensional (3D) Scatter Plot and 3D Surface Plot and how to make them with the help of Plotly.
3D Scatter Plot
三维(3D)散点图类似于散点图,但有三个变量 - x, y, and z or f(x, y) 是实数。此图形可表示为三维笛卡尔坐标系中的点。通常使用透视法(等距或透视)将其绘制在二维页面或屏幕上,使得其中一个维度似乎从页面中出现。
A three-dimensional (3D) scatter plot is like a scatter plot, but with three variables - x, y, and z or f(x, y) are real numbers. The graph can be represented as dots in a three-dimensional Cartesian coordinate system. It is typically drawn on a two-dimensional page or screen using perspective methods (isometric or perspective), so that one of the dimensions appears to be coming out of the page.
3D 散点图用于在三个轴上绘制数据点,以试图显示三个变量之间的关系。数据表中的每一行都由一个标记表示,其位置取决于其在 X, Y, and Z axes 上设置的列中的值。
3D scatter plots are used to plot data points on three axes in an attempt to show the relationship between three variables. Each row in the data table is represented by a marker whose position depends on its values in the columns set on the X, Y, and Z axes.
可以设置第四个变量以对应 markers 的 color 或 size ,从而为该图形添加另一个维度。不同变量之间的关系称为 correlation 。
A fourth variable can be set to correspond to the color or size of the markers, thus, adding yet another dimension to the plot. The relationship between different variables is called correlation.
Scatter3D trace 是 go.Scatter3D() 函数返回的图形对象。此函数的必需参数是 x, y and z ,它们中的每一个都是 list or array object 。
A Scatter3D trace is a graph object returned by go.Scatter3D() function. Mandatory arguments to this function are x, y and z each of them is a list or array object.
例如 -
For example −
import plotly.graph_objs as go
import numpy as np
z = np.linspace(0, 10, 50)
x = np.cos(z)
y = np.sin(z)
trace = go.Scatter3d(
x = x, y = y, z = z,mode = 'markers', marker = dict(
size = 12,
color = z, # set color to an array/list of desired values
colorscale = 'Viridis'
)
)
layout = go.Layout(title = '3D Scatter plot')
fig = go.Figure(data = [trace], layout = layout)
iplot(fig)
下面给出代码的输出 −
The output of the code is given below −
3D Surface Plot
曲面图是三维数据图。在曲面图中,每个点由 3 个点定义:其 latitude , longitude 和 altitude (X、Y 和 Z)。曲面图不会显示单个数据点,而是显示指定 dependent variable (Y) 与两个自变量(X 和 Z)之间的函数关系。此图形是等值线图的伴随图形。
Surface plots are diagrams of three-dimensional data. In a surface plot, each point is defined by 3 points: its latitude, longitude, and altitude (X, Y and Z). Rather than showing the individual data points, surface plots show a functional relationship between a designated dependent variable (Y), and two independent variables (X and Z). This plot is a companion plot to the contour plot.
这里有一个 Python 脚本,用于呈现简单的曲面图,其中 y array 是 x 的转置,z 计算为 cos(x2+y2)
Here, is a Python script to render simple surface plot where y array is transpose of x and z is calculated as cos(x2+y2)
import numpy as np
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
trace = go.Surface(x = x, y = y, z =z )
data = [trace]
layout = go.Layout(title = '3D Surface plot')
fig = go.Figure(data = data)
iplot(fig)
下面提到了上面解释的代码的输出 −
Below mentioned is the output of the code which is explained above −