2

We conduct experiments, our oscilloscope gives all plots on same screen though each variables is different in magnitude. Is it possible to achive same in the python using the experimental data?

My present code and output:

import random 

x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)

plt.plot(x,y1,'-r')
plt.plot(x,y2,'-g')
plt.legend(['y1','y2'])
plt.show()

enter image description here

Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

1

There is a pretty simple solution to that just use subplots

import random
import matplotlib.pyplot as plt
x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)

ax1 = plt.subplot(211)
plt.plot(x,y1,'-r')
ax2 = plt.subplot(212,sharex=ax1)
plt.plot(x,y2,'-g')
ax1.get_shared_x_axes().join(ax1, ax2)
#make x axis on upper invisible
plt.setp(ax1.get_xaxis(), visible=False)

ax1.legend(['y1'])
ax2.legend(['y2'])
plt.show()

Looks like this enter image description here

You can remove the bottom-border from the upper subplot and the upper border from the lower subplot with this:

ax1.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)

GridSpec might help you to remove margins, however I gues there should be a simpler way to remove the distance between two subplots

Björn
  • 1,610
  • 2
  • 17
  • 37
  • This looks good. Is it not possible to get plots as in the experiments? – Mainland Apr 17 '20 at 20:05
  • Judging from [this](https://stackoverflow.com/questions/20057260/how-to-remove-gaps-between-subplots-in-matplotlib) and [this](https://stackoverflow.com/questions/42850225/eliminate-white-space-between-subplots-in-a-matplotlib-figure) you might want to something with `gridspec` to remove the white margin between the subplots – Björn Apr 17 '20 at 20:08
  • Also updated my answer to also include options to remove the border between the two subplots – Björn Apr 17 '20 at 20:09
  • 1
    `plt.subplots_adjust(hspace=0.01)` or a small value should help with the margins. https://i.imgur.com/3pyhskI.png – Guimoute Apr 17 '20 at 20:12
  • @Guimoute `plt.subplots_adjust(hspace=0.01)` really made good impression by removing white spaces. It looks close to a real plot. Thanks a ton. – Mainland Apr 17 '20 at 20:18
  • how to get grid x,y axis grid lines for all axes? – Mainland Apr 17 '20 at 23:17
  • `plt.axis('off')` but then there are no axis at all, see [here](https://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces) – Björn Apr 18 '20 at 07:18