xingluo xingluo - 3 months ago 16
Python Question

How to plot a Python Dataframe with category values like this picture?

enter image description here

How can I achieve that using matplotlib?


Here is my code with the data you provided. As there's no class [they are all different, despite your first example in your question does have classes], I gave colors based on the numbers. You can definitely start alone from here, whatever result you want to achieve. You just need pandas, seaborn and matplotlib:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# import xls
# exclude Ranking values
df1 = df.ix[:,1:-1]
# for each element it takes the value of the xls cell
df2=df1.applymap(lambda x: float(x.split('\n')[1]))

# now plot it
df_heatmap = df2
fig, ax = plt.subplots(figsize=(15,15))
sns.heatmap(df_heatmap, square=True, ax=ax, annot=True, fmt="1.3f")

Which produces the following picture. result