gpt4 book ai didi

python - 如何在 plotly 中以编程方式制作子图?

转载 作者:行者123 更新时间:2023-12-04 09:34:12 26 4
gpt4 key购买 nike

我正在寻找使用 plotly 创建类似下面的内容,我刚刚开始使用该库。我可以使用下面的代码创建图形,但是我不能像图中那样将它们放在一个图形下。
enter image description here

from sklearn.datasets import load_iris
from sklearn import tree
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pdb

#n_classes = 3
#plot_colors = "ryb"
plot_step = 0.02
#pair = [0, 1]

iris = load_iris()

for index, pair in enumerate([[0, 1], [0, 2], [0, 3],[1, 2], [1, 3], [2, 3]]):
fig = make_subplots(rows=2,cols = 3)
i = (index//3)+1 #indexing for rows
k =(index//2)+1 #indexing for cols
#pdb.set_trace()
X = iris.data[:, pair]
y = iris.target
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)

x_min, x_max = X[:, 0].min(), X[:, 0].max()
y_min, y_max = X[:, 1].min(), X[:, 1].max()
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
#pdb.set_trace()

fig.add_trace(go.Contour(z = Z,
x = np.linspace(x_min,x_max,num = Z.shape[1]),
y = np.linspace(y_min,y_max,num = Z.shape[0])

),i,k)
fig.update_layout(
autosize=False,
width=1000,
height=800)
for cl in np.unique(y):
idx = np.where(y == cl)
fig.add_trace(go.Scatter(x=X[idx, 0].ravel(), y=X[idx, 1].ravel(),
mode = 'markers'),i,k)

fig.show()

最佳答案

错误的原因是绘制图形的初始设置处于循环过程中。
代码更改:

  • pair的值在一个循环过程中。
  • 将初始图形设置移出循环。
  • 更改 `add_trace() 的放置索引
  • from sklearn.datasets import load_iris
    from sklearn import tree
    import pandas as pd
    import numpy as np
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots
    import pdb

    #n_classes = 3
    #plot_colors = "ryb"
    plot_step = 0.02
    #pair = [0, 1]

    iris = load_iris()

    fig = make_subplots(rows=2,cols = 3) # update

    for index, pair in enumerate([[0, 1], [0, 2], [0, 3],[1, 1], [1, 2], [1, 3]]):
    # fig = make_subplots(rows=2,cols = 3)
    i = (index//6)+1 #indexing for rows
    k =(index//3)+1 #indexing for rows
    #pdb.set_trace()
    X = iris.data[:, pair]
    y = iris.target
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(X, y)

    x_min, x_max = X[:, 0].min(), X[:, 0].max()
    y_min, y_max = X[:, 1].min(), X[:, 1].max()
    xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
    np.arange(y_min, y_max, plot_step))

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    #pdb.set_trace()

    fig.add_trace(go.Contour(z = Z,
    x = np.linspace(x_min,x_max,num = Z.shape[1]),
    y = np.linspace(y_min,y_max,num = Z.shape[0]),
    ),row=pair[0]+1, col=pair[1])

    for cl in np.unique(y):
    idx = np.where(y == cl)
    fig.add_trace(go.Scatter(x=X[idx, 0].ravel(), y=X[idx, 1].ravel(),
    mode = 'markers'),row=pair[0]+1, col=pair[1])
    fig.update_layout(
    autosize=False,
    width=1000,
    height=800)

    fig.show()
    enter image description here

    关于python - 如何在 plotly 中以编程方式制作子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62676107/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com