英文:
How can I recreate a gridspec subplot?
问题
以下是您的翻译部分:
我需要创建一个包含一些子图的单一图形。图形必须与下面的图像完全一样:
我在绘制第二个图时遇到问题,不知道如何从其他图中获取“缩放”。我曾尝试过一些技巧,但如果我用这种方法生成第二个图,就不起作用。我应该如何绘制?以下是我所做的事情:
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()
英文:
I need to creat a single figure with some subplots. The figure must look exactly like below:
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()
答案1
得分: 3
- 使用
plt.subplots
并带有关键字参数width_ratio=[]
- 使用
ax.semilogy(x, y, ...)
进行绘图 - 在第二个子图中更改x和y的限制,然后您就可以进行缩放
英文:
Three points
- use
plt.subplots
with the keyword argumentwidth ratio=[]
- use
ax.semilogy(x, y, ...)
for plotting - change the x and y limits in the second subplot, and you have your zoom
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()
答案2
得分: 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,'r-',label="y=10x-4")
plt.plot(x,y2,'g-',label="y=x²")
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.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,'r-')
plt.plot(x,y2,'g-')
plt.grid(True)
plt.xlim(0,20)
plt.ylim(0.1,1000)
plt.yscale('log')
plt.title('Intersection:')
plot_graph()
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论