简体   繁体   中英

How to change the colours in plotly heatmap

I have the following sample dataframe matrix below which I generated using some functions I created:

             Loan_ID    Gender   Married     Dependents   Education
Loan_ID      1.000       NaN      NaN          0.000        0.000
Gender        NaN       1.000     NaN           NaN          NaN
Married      0.638      0.638    1.000         0.638        0.638
Dependents   0.000      0.000    0.000         1.000        0.000
Education    0.502      0.502    0.502         0.502        1.000

I am trying to use plotly to plot heatmap but with specific colours based on some values. Based on the dataframe. if the value is less than 0.05, I want the cell to be green if the cell is greater than 0.05 but less than 0.1, I want the colour to be green. The conditional statement will look like something below:

colorscales = []
data_mask = df_mask.to_numpy()

for row in data_mask:
    for value in row:
        if np.isnan(value):
            color = "#f8fffa"
        elif float(value) < 0.05:
            color = "#10c13b"
        elif (float(value) > 0.05) and (float(value) < 0.1):
            color = '#fac511'
        colorscales.append(color)

I want the colour showed in the plotly heatmap to be reflected by these colours. I have tried using the colorscales and also the bgcolor in the figure layout but nothing works. Any suggestions will be highly appreciated

绘图热图

I solved this problem by using a scale [0.1] with the colorscale argument like below:

colorscale = [[0, "#10c13b"],
             [0.05, '#10c13b'],
             [0.051, '#fac511'],
             [0.1, "#fac511"],
             [0.11, "#f71907"],
             [0.2, "#f71907"],
              [0.3, "#f71907"],
              [0.4, "#f71907"],
              [0.5, "#f71907"],
              [0.6, "#f71907"],
              [0.7, "#f71907"],
              [0.8, "#f71907"],
              [0.9, "#f71907"],
              [1.0, "#f71907"]]

This solved my problem efficiently, in case anyone ever needs to do this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM