I have the following dataset in a pandas DataFrame, which I've tidied and saved into "filename1.csv"
:
import pandas as pd
df = pd.read_csv("filename1.csv")
print(df)
samples a b c percent_a percent_c ratio_a:b ratio_c:b
0 sample1 185852 6509042 253303 0.028553 0.038916 35.022717 25.696664
1 sample2 218178 6456571 273448 0.033792 0.042352 29.593135 23.611696
2 sample3 251492 6353453 343252 0.039584 0.054026 25.263042 18.509588
3 sample4 232299 6431376 284522 0.036120 0.044240 27.685767 22.604143
..............................
I would like to plot this DataFrame as a heatmap using seaborn. At first, it would be interesting to see the samples (one sample per row) against two columns, percent_a
and percent_c
:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# drop unnecessary columns
df = df.drop(["a", "b", "c", "ratio_a:b", "ratio_c:b"], axis = 1)
sns.heatmap(df)
plt.show()
However, this throws an error:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs
could not be safely coerced to any supported types according to the casting rule ''safe''
I originally thought this meant that there were NaN
values in this DataFrame. However, it appears wrong, as
df.isnull().values.any()
outputs False
. So, I suspect this is because samples
is a column of non-numerical values.
How do I plot a seaborn heatmap such that these categorical values are shown?
If you just drop the "samples"
column as well, isn't that what you are looking for?! You can then put the sample names in later using matplotlib's ax.set_yticklabels
function. Note that you need to reverse sample names list, since matplotlib starts the labeling from the bottom.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("SO_pandassnsheatmap.txt", delim_whitespace=True)
df2 = df.drop(["samples", "a", "b", "c", "ratio_a:b", "ratio_c:b"], axis = 1)
ax = sns.heatmap(df2)
ax.set_yticklabels(df.samples.values[::-1])
plt.show()
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.