sephirothftw
sephirothftw

Reputation: 11

Matplotlib Secondary_y cause plot to shift right

I was hoping someone could help me with an issue.

I am trying to plot a line graph over a bar graph. Below is a link to the line graph and the code:

fig, ax = plt.subplots(figsize=(10, 5), sharex = True)
#df_plot.plot(ax = ax, y='D', kind = 'bar', legend = False)
df_plot.plot(ax = ax, secondary_y = True, y = 'E', kind = 'line', c = 'r')
plt.show()

Line Graph

And the bar graph:

fig, ax = plt.subplots(figsize=(10, 5), sharex = True)
df_plot.plot(ax = ax, y='D', kind = 'bar', legend = False)
#df_plot.plot(ax = ax, secondary_y = True, y = 'E', kind = 'line', c = 'r')
plt.show()

Bar Graph

But when I try to combine the graphs onto one figure, the line graph shifts over to the right by one.

fig, ax = plt.subplots(figsize=(10, 5), sharex = True)
df_plot.plot(ax = ax, y='D', kind = 'bar', legend = False)
df_plot.plot(ax = ax, secondary_y = True, y = 'E', kind = 'line', c = 'r')
plt.show()

Combined Graph

Does anyone know why this is happening?

Upvotes: 1

Views: 513

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

There is no "shift".
The bars are positionned such that the first bar starts at position 0, the second at 1 etc. The line graph starts at position 1; this is the position where the second bar is shown. Only due to the labeling, this appears to be wrong. But mind that the label could also read "banana" in which case one would not wonder too much about the resulting plot.

In order to overcome this, you may shift your line plot manually by 1 unit.

Upvotes: 1

Related Questions