Reputation: 2694
I have a DataFrame with 10 columns and three values {0,1,2}:
row1 = [random.randint(0,2) for i in range(10)]
row2 = [random.randint(0,2) for i in range(10)]
df = pd.DataFrame([{i:row1[i] for i in range(10)}, {i:row2[i] for i in range(10)}])
I want to plot these values in a horizonal barplot, where each value should have another color, but the same width.
First try
df.plot(kind='barh', stacked=True, legend=False)
ok, so here the width of each stacked value is not fixed. So I thought of a helper Dataframe which fixes the width to one:
df2 = pd.DataFrame([{i:1 for i in range(10)}, {i:1 for i in range(10)}])
ok, perfect, each stacked column has the same width, but now I want the colors to be either grey, blue or red, according to the values in df
Upvotes: 0
Views: 380
Reputation: 150785
I think you are searching for plt.imshow
or sns.heatmap
rather than stack barplot. For example:
sns.heatmap(df, cmap='tab10', linewidths=1, linecolor='w')
gives:
Upvotes: 1