0

I have a bar Chart plotted with Pandas and want to insert multiple y-axises. Plotting the Chart, that is generated from the code below (see Picture link), it could be seen, that the first two blocks are too small for the scaling. How can I insert multiple y-axises, that every block is displayed well ? Using axis.twinx() only the column values are assigned to single y-axis but not the whole blocks, like I want.

from matplotlib import pyplot as plt
import pandas as pd

g = ['A1', 'B2', 'C3']
params = ['one', 'two', 'three']
data = [[1, 3e-5, 400], [2, 5e-5, 300], [1.5, 4e-5, 350]]

data_dict = dict(zip(g,data))
df=pd.DataFrame(data=data_dict,index=params,columns=g)
fig0, ax0 = plt.subplots()
ax1 = ax0.twinx()

df.plot(kind='bar', ax=ax0)

fig0.show()

bar Chart plot with only one y-axis

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Lala
  • 1
  • 2
  • http://matplotlib.org/examples/api/two_scales.html – Chuck Feb 09 '17 at 14:44
  • Possible duplicate of [multiple axis in matplotlib with different scales](http://stackoverflow.com/questions/9103166/multiple-axis-in-matplotlib-with-different-scales) – Chuck Feb 09 '17 at 14:45
  • Why not use a log-scale for data that spans several orders of magnitude? `ax0.set_yscale('log')` – tmdavison Feb 09 '17 at 15:35
  • Thanks @Charles Morris, but if I use this multiple axis, the axis always depends on the different Groups, so A1 can have another y-axis than B2 and so and not for the whole blocks, like "one" have another y-axis than "two". – Lala Feb 10 '17 at 12:12

1 Answers1

1

Since the scales for the three params are so completely different, it probably makes sense to use three different subplots.

from matplotlib import pyplot as plt
import pandas as pd

g = ['A1', 'B2', 'C3']
params = ['one', 'two', 'three']
data = [[1, 3e-5, 400], [2, 5e-5, 300], [1.5, 4e-5, 350]]

data_dict = dict(zip(g,data))
df=pd.DataFrame(data=data_dict,index=params,columns=g)

fig0, ax = plt.subplots(ncols=3, figsize=(10,4))

for i, p in enumerate(params):
    df.loc[[p,p], :].plot(kind='bar', ax=ax[i], )
    ax[i].set_xlim([-.5,.5])

plt.tight_layout()
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712