0
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from io import StringIO

s = StringIO("""     amount     price
A     40929   4066443
B     93904   9611272
C    188349  19360005
D    248438  24335536
E    205622  18888604
F    140173  12580900
G     76243   6751731
H     36859   3418329
I     29304   2758928
J     39768   3201269
K     30350   2867059""")

df = pd.read_csv(s, index_col=0, delimiter=' ', skipinitialspace=True)

fig = plt.figure() # Create matplotlib figure

ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.

width = 0.4

df.amount.plot(kind='bar', color='red', ax=ax, width=width, position=1)
df.price.plot(kind='bar', color='blue', ax=ax2, width=width, position=0)

ax.set_ylabel('Amount')
ax2.set_ylabel('Price')

plt.show()
Epsi95
  • 8,832
  • 1
  • 16
  • 34
rose
  • 11
  • I wanted to add the value labels above the bars corresponding to the correct axis. – rose Mar 03 '21 at 08:01
  • In this case, this is a duplicate: https://stackoverflow.com/q/28931224/8881141 – Mr. T Mar 03 '21 at 08:55
  • I think that the solution is not that easy in this case, since there are two axis with different labels. I would be very interested in including the value labels corresponding to the correct axis in the bar chart. Thank's for help. – rose Mar 06 '21 at 05:34
  • I understand your concern but [this answer](https://stackoverflow.com/a/48372659/8881141) implemented how to identify all bars in an axis object - so you just have to call it with each of the axes. But your real problem is that the values are so high that you will encounter space problems. Maybe horizontal bars will be easier to implement. – Mr. T Mar 06 '21 at 09:07
  • Thank's i got it. You helped a lot! – rose Mar 06 '21 at 09:18

0 Answers0