You need to enable JavaScript to run this app. 用Plotly Express基于两个多列pandas DataFrame绘制x/y分离散点图
优惠活动
大模型
产品
解决方案
定价
更多

用Plotly Express基于两个多列pandas DataFrame绘制x/y分离散点图

散点图实现代码

方案1:Plotly实现(支持交互式查看,原生支持trace定义)

import pandas as pd
import plotly.graph_objects as go

# 示例数据,可替换为你自己的x_df、y_df
x_df = pd.DataFrame({"red": [1,2,3], "blue": [2,3,4]})
y_df = pd.DataFrame({"red": [1,2,3], "blue": [2,3,4]})

fig = go.Figure()
# 添加red系列
fig.add_trace(go.Scatter(
    x=x_df["red"],
    y=y_df["red"],
    mode="markers",
    name="red",
    marker={"color": "red", "size": 8}
))
# 添加blue系列
fig.add_trace(go.Scatter(
    x=x_df["blue"],
    y=y_df["blue"],
    mode="markers",
    name="blue",
    marker={"color": "blue", "size": 8}
))

# 自定义坐标轴和图例
fig.update_layout(
    xaxis_title="X轴数值",
    yaxis_title="Y轴数值",
    legend_title="数据系列"
)
fig.show()

方案2:Matplotlib实现(适合静态出图)

import pandas as pd
import matplotlib.pyplot as plt

x_df = pd.DataFrame({"red": [1,2,3], "blue": [2,3,4]})
y_df = pd.DataFrame({"red": [1,2,3], "blue": [2,3,4]})

plt.scatter(x_df["red"], y_df["red"], c="red", label="red", s=50)
plt.scatter(x_df["blue"], y_df["blue"], c="blue", label="blue", s=50)
plt.xlabel("X轴数值")
plt.ylabel("Y轴数值")
plt.legend()
plt.show()

运行以上任意代码即可得到符合要求的双系列散点图,marker大小、颜色等参数可根据你的实际需求调整。

内容的提问来源于stack exchange,提问作者DDStackoverflow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 03:39:04