如何在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
相关产品推荐
相关产品推荐

