1

I have a dictionary of dictionaries called data. I want to plot a bar chart so that each of A, B, C, and D have a value for the pos and neg.

I don't want the pos and neg bars to be stacked on top of each other. I want them side by side.

Additionally, I want the categories to be sorted by descending order of their total frequency (pos + neg)

data = {'A': {'pos': 289794, 'neg': 515063},
        'B': {'pos': 174790, 'neg': 292551},
        'C': {'pos': 375574, 'neg': 586616},
        'D': {'pos': 14932, 'neg': 8661}}
piccolo
  • 2,093
  • 3
  • 24
  • 56

1 Answers1

2
%matplotlib inline 
import matplotlib.pyplot as plt
import pandas as pd

data = {'A': {'pos': 289794, 'neg': 515063},
        'B': {'pos': 174790, 'neg': 292551},
        'C': {'pos': 375574, 'neg': 586616},
        'D': {'pos': 14932, 'neg': 8661}}
df = pd.DataFrame(data)
df = df.T
df ['sum'] = df.sum(axis=1)
df.sort_values('sum', ascending=False)[['neg','pos']].plot.bar() 

enter image description here

ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45