Search code examples
pythonplotlyhexscatter-plot

How to manually specify HEX colors in categorial scatter plot using Plotly express?


I am trying to change the colors to my scatter plot using hex codes. I need to assign the following hex color codes to my 3 categories: "#581D74", "#4BA8B6" and "A7C673"

This is the code i have used: For now I used a discrete pallette available on Plotly express

fig9 = px.scatter(df, x="ApplicantIncome", y="LoanAmount", color ="Property_Area", trendline = "ols",
                  color_discrete_sequence=px.colors.qualitative.Prism,
                  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()

and the output is okay but i really need the exact hex color codes, any ideas ?


Solution

  • Use the hex color codes in a list as follows:

    import pandas as pd
    import plotly.express as px
    
    df = px.data.gapminder().query("year == 2007")
    
    fig = px.bar(df, y="continent", x="pop", color="continent", orientation="h", hover_name="country",
                 color_discrete_sequence=["#581D74", "#4BA8B6", "#A7C673","#581D74", "#4BA8B6"],
                 title="Explicit color sequence"
                )
    
    fig.show()
    

    Output:

    enter image description here