matplotlib 如何重新创建网格规格子图?

kmbjn2e3  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(132)

我需要创建一个单一的数字与一些子情节。该图必须与下图完全相同:

我在绘制第二个图时遇到了问题,我不知道如何从另一个图中获得“缩放”,我使用了技巧,但如果我使用这个来生成第二个图,则不起作用。我怎么能阴谋?这就是我所做的:

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()
t3psigkw

t3psigkw1#

三分
1.使用plt.subplots和关键字参数width ratio=[]
1.使用ax.semilogy(x, y, ...)进行绘图
1.在第二个子图中改变x和y的限制,你就可以缩放了

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()
vybvopom

vybvopom2#

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()

输出:

相关问题