1

So I am trying to plot a data set that looks something like this:

cat    a        b        c
idk    0.47     0.58     1.17
yeet   0.38     0.11     -0.8
skeet  0.03     0.14     0.46

Basically, cat is a categorical variable, a and b are variables which are in a range of [0,1] while c is a variable which ranges from [-7,7] and I want a plot where the x axis is the categorical variable with the left y axis being [0,1] corresponding to a&b while the right y axis corresponds to c.

I can plot all of them with the same y axis, which obviously is not ideal and in trying to plot the different y axes with

fig = plt.figure() 
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx() 
width = 0.4
wisco_ill_succ.plot.bar(x='cat', y=['a','b'], ax = ax1, position = 0, width = width)
wisco_ill_succ.plot.bar(x='cat', y=['c'], ax = ax2, position = 1, width = width)

I get what I want, however the x axes do not line up and it just looks like a jumbled mess: ew

I looked at this Pandas: Bar-Plot with two bars and two y-axis but frankly it has not been very helpful. Is there anything I can do or should I just split it up into two different graphs?

r-beginners
  • 31,170
  • 3
  • 14
  • 32
mcqconor
  • 25
  • 5

1 Answers1

3

You can use the secondary_y argument:

ax = df.plot.bar(secondary_y=['c'], x='cat', rot=0)
ax.set_ylim(-1,1)
ax.right_ax.set_ylim(-7,7)

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52
  • This worked for me. Very straightforward coding. Just a note that you have to run the three lines at the same time, was not working when I was testing line-by-line. – Hasnein Tareque Feb 26 '21 at 05:56