Ghi chú Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code Trình diễn một phân tán cơ bản trong 3D.import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed[19680801]
def randrange[n, vmin, vmax]:
"""
Helper function to make an array of random numbers having shape [n, ]
with each number distributed Uniform[vmin, vmax].
"""
return [vmax - vmin]*np.random.rand[n] + vmin
fig = plt.figure[]
ax = fig.add_subplot[projection='3d']
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [['o', -50, -25], ['^', -30, -5]]:
xs = randrange[n, 23, 32]
ys = randrange[n, 0, 100]
zs = randrange[n, zlow, zhigh]
ax.scatter[xs, ys, zs, marker=m]
ax.set_xlabel['X Label']
ax.set_ylabel['Y Label']
ax.set_zlabel['Z Label']
plt.show[]
Phòng trưng bày được tạo ra bởi Sphinx-Gallery
Sử dụng mã sau nó hoạt động cho tôi:
# Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
Trong khi x_iso là mảng 3-D của tôi và cho x_vals, y_vals, z_vals tôi đã sao chép/sử dụng 1 cột/trục từ mảng đó và được gán cho các biến/mảng đó tương ứng.
In [1]:
import plotly.express as px df = px.data.iris[] fig = px.scatter_3d[df, x='sepal_length', y='sepal_width', z='petal_width', color='species'] fig.show[]
Một chiều thứ 4 của dữ liệu có thể được biểu diễn nhờ màu của các điểm đánh dấu. Ngoài ra, các giá trị từ cột species
được sử dụng bên dưới để gán các ký hiệu cho các điểm đánh dấu.
In [2]:
import plotly.express as px df = px.data.iris[] fig = px.scatter_3d[df, x='sepal_length', y='sepal_width', z='petal_width', color='petal_length', symbol='species'] fig.show[]
Phong cách 3D phân tán cốt truyện
Có thể tùy chỉnh kiểu của hình thông qua các tham số của px.scatter_3d
cho một số tùy chọn hoặc bằng cách cập nhật các dấu vết hoặc bố cục của hình thông qua
# Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
0.In [3]:
import plotly.express as px df = px.data.iris[] fig = px.scatter_3d[df, x='sepal_length', y='sepal_width', z='petal_width', color='petal_length', size='petal_length', size_max=18, symbol='species', opacity=0.7] # tight layout fig.update_layout[margin=dict[l=0, r=0, b=0, t=0]]
Các lô phân tán 3D trong Dash¶
Dash là cách tốt nhất để xây dựng các ứng dụng phân tích trong Python bằng cách sử dụng các số liệu thuần túy. Để chạy ứng dụng bên dưới, chạy
# Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
1, nhấp vào "Tải xuống" để lấy mã và chạy # Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
2.Bắt đầu với các tài liệu Dash chính thức và tìm hiểu cách dễ dàng tạo kiểu và triển khai các ứng dụng như thế này với Dash Enterprise.learn how to effortlessly style & deploy apps like this with Dash Enterprise.
Đăng ký Câu lạc bộ Dash → Bảng gian lận miễn phí cộng với các bản cập nhật từ Chris Parmer và Adam Schroeder đã gửi đến hộp thư đến của bạn hai tháng một lần. Bao gồm các mẹo và thủ thuật, ứng dụng cộng đồng và lặn sâu vào kiến trúc Dash. Tham gia ngay. → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.
In [5]:
import plotly.graph_objects as go import numpy as np # Helix equation t = np.linspace[0, 10, 50] x, y, z = np.cos[t], np.sin[t], t fig = go.Figure[data=[go.Scatter3d[x=x, y=y, z=z, mode='markers']]] fig.show[]
Biểu đồ phân tán 3D với phong cách đánh dấu màu sắc và đánh dấu
In [6]:
import plotly.graph_objects as go import numpy as np # Helix equation t = np.linspace[0, 20, 100] x, y, z = np.cos[t], np.sin[t], t fig = go.Figure[data=[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', # choose a colorscale opacity=0.8 ] ]]] # tight layout fig.update_layout[margin=dict[l=0, r=0, b=0, t=0]] fig.show[]
Còn Dash thì sao? ¶
Dash là một khung nguồn mở để xây dựng các ứng dụng phân tích, không cần JavaScript và nó được tích hợp chặt chẽ với thư viện đồ thị đồ họa.
Tìm hiểu về cách cài đặt Dash tại //dash.plot.ly/installation.
Ở mọi nơi trong trang này mà bạn thấy
# Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
3, bạn có thể hiển thị cùng một hình trong một ứng dụng Dash bằng cách chuyển nó sang đối số # Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
4 của thành phần # Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
5 từ gói # Create the figure
fig = plt.figure[]
ax = fig.add_subplot[111, projection='3d']
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter[x_vals, y_vals, z_vals, c = 'b', marker='o']
ax.set_xlabel['X-axis']
ax.set_ylabel['Y-axis']
ax.set_zlabel['Z-axis']
plt.show[]
6 tích hợp như thế này:import plotly.graph_objects as go # or plotly.express as px fig = go.Figure[] # or any Plotly Express function e.g. px.bar[...] # fig.add_trace[ ... ] # fig.update_layout[ ... ] import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash[] app.layout = html.Div[[ dcc.Graph[figure=fig] ]] app.run_server[debug=True, use_reloader=False] # Turn off reloader if inside Jupyter