3

I can build a simple plot in a matplotlib according to the 'simple' dictionary:

import matplotlib.pyplot as plt
D = {u'Label1':26, u'Label2': 17, u'Label3':30}
plt.bar(range(len(D)), D.values(), align='center')
plt.xticks(range(len(D)), D.keys())

enter image description here

But, how do I create two graphics on the data of these dictionaries on one the plot I do not know?

NG1={'need1': [{'good1': 3, 'good2': 4}], 'need2': [{'good2': 2, 'good3': 2}]}
NG2={'need1': [{'good1': 13, 'good2': 23}], 'need2': [{'good2': 8, 'good3': 14}]}

Like the picture below

enter image description here

2
  • 1
    What is the desired result? What do you expect the graph to look like? Commented Oct 17, 2017 at 13:43
  • Hi! Tell me, this is possible to create or not, using matplotlib? Commented Oct 17, 2017 at 15:22

2 Answers 2

5

using pandas you can do approximately what I think you want

NG1={'need1': {'good1': 3, 'good2': 4}, 'need2': {'good2': 2, 'good3': 2}}
NG2={'need1': {'good1': 13, 'good2': 23}, 'need2': {'good2': 8, 'good3': 14}}

(notice the lack of [])

combined_df = pd.concat({'ng1': pd.DataFrame(NG1), 'ng2': pd.DataFrame(NG2)}).unstack(0)
combined_df
   need1            need2
    ng1     ng2     ng1     ng2
good1   3.0     13.0    NaN     NaN
good2   4.0     23.0    2.0     8.0
good3   NaN     NaN     2.0     14.0

Depending on what exactly it is you want you can omit the unstack

combined_df.plot.bar()

delivers

bar plot

edit update

I cannot create exactly what you need in this way, You would need to play around with different glyphs and figures and I don't have the skills or time to do this, but I can provide the data in the correct way

combined_df = pd.concat({'ng1': pd.DataFrame(NG1), 'ng2': pd.DataFrame(NG2)}).stack()
combined_df.index.names = ['ng', 'good', 'need']
combined_df = combined_df.unstack(['good'])
combined_df['sum'] = combined_df.sum(axis=1)
combined_df
    good    good1   good2   good3   sum
ng  need                
ng1     need1   3.0     4.0     NaN     7.0
need2   NaN     2.0     2.0     4.0
ng2     need1   13.0    23.0    NaN     36.0
need2   NaN     8.0     14.0    22.0
combined_df.plot.bar()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer! I add the question with the figure below. Tell me this is possible to create or not?
And how to make two drawings : ng1 (need1, need2) and ng2 (need1, need2) on the one picture. Is this possible?
3

Adding to @MaartenFabré 's solution, you can get the sum as another bar plot in the background by plotting the sum as an individual bar plot and make it unfilled,

combined_df['sum'].plot.bar(zorder=0, fill=False)

Complete solution:

import matplotlib.pyplot as plt
import pandas as pd

NG1={'need1': {'good1': 3, 'good2': 4}, 'need2': {'good2': 2, 'good3': 2}}
NG2={'need1': {'good1': 13, 'good2': 23}, 'need2': {'good2': 8, 'good3': 14}}

combined_df = pd.concat({'ng1': pd.DataFrame(NG1), 'ng2': pd.DataFrame(NG2)}).stack()
combined_df.index.names = ['ng', 'good', 'need']
combined_df = combined_df.unstack(['good'])

combined_df.plot.bar()

combined_df['sum'] = combined_df.sum(axis=1)
combined_df['sum'].plot.bar(zorder=0, fill=False)


plt.show()

enter image description here

1 Comment

And how to make two drawings : ng1 (need1, need2) and ng2 (need1, need2) on the one picture. Is this possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.