2017-06-09 4 views
0

Ich versuche, eine Heatmap, die in HTML gelesen werden kann, es gibt derzeit die Daten ordnungsgemäß aus, aber die Heatmap Färbung basiert auf dem Maximalwert in jeder Spalte nicht auf den Maximalwert in der Ich bin mir nicht sicher, wie ich das ändern soll, also basiert die Heatmap auf allen Daten.Python Heatmap Tabelle Coloring Problem

from flask import * 
import pandas as pd 
import seaborn as sns 

app = Flask(__name__) 

@app.route("/Matrix") 
def show_tables(): 
    cm = sns.light_palette("red", as_cmap=True) 
    df = pd.read_csv('matrix.csv', header=0, index_col=0, na_filter=False) 
    df = df.sort_index(ascending=False) 
    html = (df.style.background_gradient(cmap=cm).render()) 
    return render_template('view.html', tables=[html]) 
if __name__ == "__main__": 
    app.run(debug=True) 

Heatmap Tabelle Ausgang:

Heatmap Table Output

+0

Hallo Alex. Hat meine Antwort dein Problem gelöst? Wenn ja, ist es üblich, die Antwort zu akzeptieren, damit die Frage als gelöst markiert wird. –

Antwort

1

Sie werden von abbrechen wollen, was Sie tun und gehen lesen Sie die Styling Dokumentation für pandas (http://pandas.pydata.org/pandas-docs/stable/style.html)

Kurz gesagt, Sie müssen Ihre eigene Style-Funktion schreiben (modifiziert aus der Dokumentation)

import pandas as pd 
import numpy as np 
import seaborn as sns 

df = pd.DataFrame(np.random.random(size=(10, 5))) 

def highlight_max(data): 
    cm = sns.light_palette("red", as_cmap=True) 

    # have to convert the color map colors to hex values for the CSS to work 
    # if you have a better way of doing the RGBA -> HEX conversion use that 
    # this is awful but works 
    hex_colors = ['#{:02x}{:02x}{:02x}'.format(*(np.asarray(cm(v)) * 255).astype(int)) for v in data.values.ravel()] 
    colors = ['background-color: {}'.format(h) for h in hex_colors] 
    return pd.DataFrame(np.asarray(colors).reshape(*data.shape), 
         index=data.index, 
         columns=data.columns) 


df.style.apply(highlight_max, axis=None) # axis none to apply to whole DF in one go