I am trying to make a heatmap that can be read in html, it is currently outputting the data properly, but the heatmap coloring is based off the max value in each column not on the max value in the data I am not sure how to go about changing this so the heatmap is based on all the data.
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 Table Output:
You'll want to break off from what you're doing and go read the Styling documentation for pandas
( http://pandas.pydata.org/pandas-docs/stable/style.html )
In short you'll need to write your own style function (modified from the documentation)
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
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.