如何从Python中的情节子图中删除多个传说

发布于 2025-01-31 07:23:17 字数 1294 浏览 4 评论 0原文

我正在研究虹膜数据集。要拥有与我相同的数据使用此代码 -

from sklearn.datasets import load_iris

data = load_iris()

features_df = pd.DataFrame(data.data, columns = data.feature_names)
target = pd.DataFrame(data.target, columns = ["species"])
target.replace(dict(zip(np.unique(data.target), data.target_names)), inplace = True)

df = pd.concat([features_df, target], axis = 1)

使用此代码,我能够生成4个散点图作为子图 -

fig = make_subplots(rows = 2, cols = 2)

plots_xy = [("sepal length (cm)", "petal length (cm)"), ("sepal width (cm)", "petal width (cm)"), ("sepal length (cm)", "sepal width (cm)"), ("petal length (cm)", "petal width (cm)")]
plot_coords = list(itertools.product([1,2], repeat = 2))

for index, plot in enumerate(plots_xy):
  fig.add_traces(px.scatter(data_frame = df, x = plot[0], y = plot[1], color = "species").data, rows = plot_coords[index][0], cols = plot_coords[index][1])
  fig.update_xaxes(title_text = plot[0])
  fig.update_yaxes(title_text = plot[1])

fig.update_layout(width = 1200, height = 900)
fig.show()

绘图 -

”

如您所见,传说在这里重复。我该如何停止这个,只有一个传奇人物?

I am working on the Iris Dataset. To have the same data as me use this code -

from sklearn.datasets import load_iris

data = load_iris()

features_df = pd.DataFrame(data.data, columns = data.feature_names)
target = pd.DataFrame(data.target, columns = ["species"])
target.replace(dict(zip(np.unique(data.target), data.target_names)), inplace = True)

df = pd.concat([features_df, target], axis = 1)

Using this code I am able to generate 4 scatterplots as subplots -

fig = make_subplots(rows = 2, cols = 2)

plots_xy = [("sepal length (cm)", "petal length (cm)"), ("sepal width (cm)", "petal width (cm)"), ("sepal length (cm)", "sepal width (cm)"), ("petal length (cm)", "petal width (cm)")]
plot_coords = list(itertools.product([1,2], repeat = 2))

for index, plot in enumerate(plots_xy):
  fig.add_traces(px.scatter(data_frame = df, x = plot[0], y = plot[1], color = "species").data, rows = plot_coords[index][0], cols = plot_coords[index][1])
  fig.update_xaxes(title_text = plot[0])
  fig.update_yaxes(title_text = plot[1])

fig.update_layout(width = 1200, height = 900)
fig.show()

The plot -

enter image description here

As you can see, the legends are getting repeated here. How can I stop this and only have one legend for the whole plot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2025-02-07 07:23:17
names = set() fig.for_each_trace(lambda trace: trace.update(showlegend=False) if (trace.name in names) else names.add(trace.name))
names = set() fig.for_each_trace(lambda trace: trace.update(showlegend=False) if (trace.name in names) else names.add(trace.name))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文