Hướng dẫn how do you plot a 3d scatter in python? - làm thế nào để bạn vẽ một phân tán 3d trong python?

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.

Hướng dẫn how do you plot a 3d scatter in python? - làm thế nào để bạn vẽ một phân tán 3d trong python?

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 https://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

Hướng dẫn how do you plot a 3d scatter in python? - làm thế nào để bạn vẽ một phân tán 3d trong python?

Làm thế nào để bạn tạo ra một âm mưu phân tán 3 chiều?

Tạo biểu đồ Sau khi thêm dữ liệu, đi đến phần 'Dấu vết' trong menu 'Cấu trúc' ở phía bên trái. Chọn 'Loại' của dấu vết, sau đó chọn 'Phân tán 3D' trong loại biểu đồ '3D'. Tiếp theo, chọn các giá trị 'x', 'y' và 'z' từ các menu thả xuống. Điều này sẽ tạo ra một dấu vết phân tán 3D, như được thấy dưới đây.After adding data, go to the 'Traces' section under the 'Structure' menu on the left-hand side. Choose the 'Type' of trace, then choose '3D Scatter' under '3D' chart type. Next, select 'X', 'Y' and 'Z' values from the dropdown menus. This will create a 3D scatter trace, as seen below.

Làm thế nào để bạn vẽ đồ thị 3D trong Python?

Âm mưu ba chiều trong matplotlib..
Từ mpl_toolkits nhập mplot3d ..
%matplotlib inline nhập khẩu numpy khi nhập np matplotlib.pyplot như plt ..
FIG = plt. Hình () ax = plt. ....
FIG = plt. Hình () ax = plt. ....
cây rìu. View_init (60, 35) Hình. ....
FIG = plt. Hình () ax = plt. ....
cây rìu. View_init (60, 35) Hình. ....
AX = PLT. trục (chiếu = '3D') ax. ....

Làm thế nào để bạn vẽ một mảng 3D trong Python?

Matplotlib với Python..
Tạo một hình mới hoặc kích hoạt một hình hiện tại bằng phương thức Hình () ..
Thêm một '~.trục.....
Tạo một dữ liệu ngẫu nhiên có kích thước = (3, 3, 3) ..
Trích xuất dữ liệu X, Y và Z từ mảng 3D ..
Biểu đồ các điểm phân tán 3D trên trục được tạo ..
Để hiển thị hình, sử dụng phương thức show () ..

Làm thế nào để bạn tạo ra một cốt truyện phân tán 3D trong Notebook Jupyter?

Tạo một hình mới hoặc kích hoạt một con số hiện có ..
Tạo các biến FIG và AX bằng phương thức Subplots, trong đó các NROW và NCOL mặc định là 1, Projection = '3D, ..
Nhận X, Y và Z bằng NP.cos và np.....
Vẽ khung dây 3D, sử dụng x, y, z và color = "màu đỏ" ..
Đặt tiêu đề thành trục hiện tại ..
Để hiển thị hình, sử dụng PLT ..