3

I want to plot time-series on a double y-axis with matplotlib and rescale the axis so that the origin (y=0) is aligned for both axis.

I have looked at this question but the suggested solutions can cut some data out of the plot.

For example:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

np.random.seed(1)
df = pd.DataFrame(np.random.randn(100, 3), columns=['A', 'B', 'C']).cumsum()
df['Total'] = df[['A', 'B', 'C']].sum(1)

fig, ax = plt.subplots(1)
df.drop('Total', axis=1).plot(ax=ax)
ax2 = ax.twinx()
df['Total'].plot(ax=ax2, color='k', linestyle='--')

enter image description here

Using @drevicko solution (from the link above), I obtain the chart below. The origin for the two axis is now aligned, but part of the green line is left out of the plot.

How can I align the origin and leave all data in the plot frame?

enter image description here

2
  • 1
    How about using ylim to define your y scale limits as desired? Commented Aug 9, 2018 at 11:51
  • 2
    Did you try this answer which claims to solve this problem? Commented Aug 9, 2018 at 12:05

0

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.