2

I need to creat a single figure with some subplots. The figure must look exactly like below:

enter image description here

I'm having a problem to plot the second graph, I don't know how to get a "zoom" from the other graph, I was using tricks but if I use this to generate the second graph, doesn't work. How can I plot? This is what I did:

import matplotlib.pyplot as plt

def plot_graph():
   
   x = list(range(0,1000))
   y1 = [10*a-4 for a in x]
   y2 = [a**2 for a in x]

   
   plt.subplot2grid((2,2), (0,0))
   plt.plot(x,y1,y2)
   plt.xticks(list(range(0,120,20)), labels=[0,200,400,600,800,1000])
   plt.xlim(0,100)
   plt.yticks([1,100,1000,10000,100000])
   plt.ylim(1,100000)
   plt.grid(True)
   plt.plot(x,y1, "r-", label = "y=10.x-4")
   plt.plot(x,y2, "g-", label = "y=x²")
   plt.legend(loc = "lower right")
   plt.yscale('log')
   plt.xlabel('X')
   plt.ylabel('Y')
   plt.title('Functions:')

   plt.subplot2grid((2,2), (0,1))
   plt.plot(x,y1,y2)
   plt.grid(True)
   plt.plot(x,y1, "r-")
   plt.plot(x,y2, "g-")
   plt.yscale('log')
   plt.title('Intersection:')

plot_graph()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Maria_
  • 31
  • 3

2 Answers2

3

Three points

  1. use plt.subplots with the keyword argument width ratio=[]
  2. use ax.semilogy(x, y, ...) for plotting
  3. change the x and y limits in the second subplot, and you have your zoom

enter image description here

from matplotlib.pyplot import show, subplots
f, (a0, a1) = subplots(figsize=(10,2.5),
                       ncols=2,
                       width_ratios=[3, 1],
                       layout='tight')

x  = [a/10 for a in range(0,1000)]
y1 = [10*a-4 for a in x]
y2 = [a**2 for a in x]

a0.semilogy(x, y1, label='y1', lw=2, color='red')
a0.semilogy(x, y2, label='y2', lw=2, color='green')
a0.legend()
a0.grid(1)

a1.semilogy(x, y1, label='y1', lw=2, color='red')
a1.semilogy(x, y2, label='y2', lw=2, color='green')
a1.grid(1, 'both')
a1.set_xlim((5, 15))
a1.set_ylim((60, 150))

show()
gboffi
  • 22,939
  • 8
  • 54
  • 85
0
import matplotlib.pyplot as plt

def plot_graph():
   
   x = list(range(0,1000))
   y1 = [10*a-4 for a in x]
   y2 = [a**2 for a in x]

   
   plt.subplot2grid((2,2), (0,0))
   plt.plot(x,y1,y2)
   plt.xticks(list(range(0,400,100)), labels=[0,100,200,300])
   plt.xlim(0,300)
   plt.yticks([1,100,1000,10000,100000])
   plt.ylim(1,1000000)
   plt.grid(True)
   plt.plot(x,y1, "r-", label = "y=10.x-4")
   plt.plot(x,y2, "g-", label = "y=x²")
   plt.legend(loc = "lower right")
   plt.yscale('log')
   plt.xlabel('X')
   plt.ylabel('Y')
   plt.title('Functions:')

   plt.subplot2grid((2,2), (0,1))
   plt.plot(x,y1,y2)
   plt.grid(True)
   plt.plot(x,y1, "r-")
   plt.plot(x,y2, "g-")
   
   plt.xlim(0,20)
   plt.ylim(0.1,1000)
   
   plt.yscale('log')
   plt.title('Intersection:')

plot_graph()

output:

enter image description here

pippo1980
  • 2,181
  • 3
  • 14
  • 30