You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Plotly Express分类散点图中手动指定HEX颜色?

解决方案

直接将color_discrete_sequence参数的值替换为你指定的HEX颜色列表即可,无需依赖Plotly自带的调色板。

修改后的完整代码:

fig9 = px.scatter(df, x="ApplicantIncome", y="LoanAmount", color ="Property_Area", trendline = "ols",
                  color_discrete_sequence=["#581D74", "#4BA8B6", "#A7C673"],  # 替换为指定HEX颜色
                  labels={
                    "ApplicantIncome": "Applicant Monthy Income",
                     "LoanAmount": "Loan Amount",
                     "Property_Area":"Property Area",
                 },
              title="Loan Amounts Based on Applicants Income and Property Area")

fig9.update_layout({
    'plot_bgcolor': 'rgba(255,255,255,255)',
    'paper_bgcolor': 'rgba(255,255,255,255)'
})

fig9.show()

补充说明

  • color_discrete_sequence会按照Property_Area分类的默认出现顺序,依次分配列表中的颜色。
  • 如果需要强制指定某类对应某颜色,可改用color_discrete_map参数,示例如下:
    color_discrete_map={
        "Urban": "#581D74",
        "Rural": "#4BA8B6",
        "Semiurban": "#A7C673"
    }
    
    这种方式能确保分类与颜色精准绑定,不受数据中分类顺序的影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 18:43:12