Skip to content
Advertisement

how to plot different plot in a single plot matplotlib

I created 2 different plot in this way:

JavaScript

What I would like to do is to ‘collect’ these 2 plots in a single one. I try these solutions:

1:

JavaScript

2:

JavaScript

but anytime I got 3 different figures: one figures with 2 axes but empty enter image description here and 2 figures with the right plot but not where I wanted to be

enter image description here

enter image description here

Can someone help and suggest what I get wrong in my code? thanks

Advertisement

Answer

Here is a simple example of how you may proceed to achieve what you aim.

Lets create a function having axe switch to cope with existing matplotlib axe or create it if missing.

JavaScript

Similarly we create the second function:

JavaScript

The we create some synthetic data for display purpose:

JavaScript

Then comes the magic, we create both axes and send references to their respective functions:

JavaScript

Afterward we are still able to add features to axes:

JavaScript

Final result looks like:

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement