Reputation: 3
If I have two DataFrames that have the same x and y axis, how can I plot the values of the df1
on a seaborn heatmap style grid, but have the colors of each cell correspond to the values in the df2
?
Here are the two heatmaps that I want to combine into a single heatmap:
df1
df2
I have tried to base the cmap off df2
but the colors still seem to be related to the df1
.
Upvotes: 0
Views: 400
Reputation: 80449
You can call sns.seaborn(data=dataframe_for_color, annot=dataframe_for_texts, ...)
.
Here is an example, using the number of passengers to fill in the annotations, and a coloring scheme based on year and month values:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
flights = sns.load_dataset('flights')
passengers = flights.pivot(index='year', columns='month', values='passengers')
coloring_np = np.fromfunction(lambda year, month: (year % 4) * 12 + (month * 7) % 12, passengers.shape, dtype=int)
coloring_df = pd.DataFrame(coloring_np, index=passengers.index, columns=passengers.columns)
ax = sns.heatmap(data=coloring_df, annot=passengers, fmt='.0f', cmap='turbo', cbar=False)
plt.show()
Upvotes: 0