I have a pandas dataframe, df1, that looks like this:
Sample_names esv0 esv1 esv2 esv3 ... esv918 esv919 esv920 esv921
pr1gluc8NH1 635 0 6222 0 ... 0 0 0 0
pr1gluc8NH2 3189 75 9045 0 ... 0 0 0 0
pr1gluc8NHCR1 0 2152 12217 0 ... 0 0 0 0
pr1gluc8NHCR2 0 17411 1315 0 ... 0 1 0 0
pr1sdm8NH1 365 7 4117 32 ... 0 0 0 0
pr1sdm8NH2 4657 18 13520 0 ... 0 0 0 0
pr1sdm8NHCR1 0 139 3451 0 ... 0 0 0 0
pr1sdm8NHCR2 1130 1439 4163 0 ... 0 0 0 0
As you can see there are many zero values. I want to plot a stacked bar graph:
df1.plot(kind='bar',stacked=True)
This works fine and gives the right bar graph. But the legend is huge because it creates a legend for all the 922 values. There are only about 40-50 non-zero values for each Sample_names; so in principle the legend can be smaller. Is there any way to make it print the legend for only the non-zero values? I would appreciate any help.
Note: If it helps, I have created a dictionary where each element is a dataframe of one sample_names and its non-zero columns. For example, my dictionary v has 8 elements, each of which is a dataframe. v[0] looks like
Index pr1gluc8NH1
esv2 6222
esv9 4879
esv27 2050
and so on (it has 43 non-zero rows).
v[1] is the same way, but for the next sample. I could also use this dictionary to make the plots if it's possible.