[英]how to make subplots programmatically in plotly?
我希望使用 plotly 创建类似下面的内容,我刚刚开始使用该库。 我可以使用下面的代码创建数字,但是我不能将它们放在一个数字下,如图所示。
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
的值。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()
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
plot_step = 0.02
iris = load_iris()
fig = make_subplots(rows=2,cols = 3)
for index, pair in enumerate([[0, 1], [0, 2], [0, 3],[1, 2], [1, 3], [2, 3]]):
i = (index//3)+1 #indexing for rows
k = (index%3)+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()-1, X[:, 0].max()+1
y_min, y_max = X[:, 1].min()-1, X[:, 1].max()+1
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)
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 = i,col = k)
for cl,cl_name in enumerate(iris.target_names):
idx = np.where(y == cl)
fig.add_trace(go.Scatter(x=X[idx, 0].ravel(), y=X[idx, 1].ravel(),
mode = 'markers',
name = cl_name,
#legendgroup="group1",
#marker=dict(color = ''),
showlegend=False),row = i,col = k)
#pdb.set_trace()
fig.update_layout(
autosize=False,
width=1000,
height=800)
fig.show()
fig.write_html('plotly101.html')
make_subplots
和update_layout
移到了外面i
和k
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.