Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Superpose two graphs where x-axis coincide

I want to superpose two graphs where x-axis corresponds. The first is on the full range, while second is upon a sub-interval.

test1 = pd.DataFrame(
    {
        'x': [1,2,3,4,5,6,7,8,9],
        'y': [0,1,1,2,1,2,1,1,1]
    }
)

test2 = pd.DataFrame(
    {
        'x': [1,2,4,5,8],
        'y': [3,2,2,3,3]
    }
)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use the xlim() function in matplotlib.

Example:

import matplotlib.pyplot as plt
import pandas as pd

test1 = pd.DataFrame(
    {
        'x': [1,2,3,4,5,6,7,8,9],
        'y': [0,1,1,2,1,2,1,1,1]
    }
)

test2 = pd.DataFrame(
    {
        'x': [1,2,4,5,8],
        'y': [3,2,2,3,3]
    }
)

plt.plot(test1['x'], test1['y'], 'b-', label='test1')
plt.plot(test2['x'], test2['y'], 'r-', label='test2')
plt.xlim(min(test1['x']), max(test1['x']))
plt.legend()
plt.show()

Result: https://i.stack.imgur.com/bz41W.png

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading