如何重新创建一个gridspec subplot?

huangapple go评论116阅读模式
英文:

How can I recreate a gridspec subplot?

问题

以下是您的翻译部分:

我需要创建一个包含一些子图的单一图形。图形必须与下面的图像完全一样:

如何重新创建一个gridspec subplot?

我在绘制第二个图时遇到问题,不知道如何从其他图中获取“缩放”。我曾尝试过一些技巧,但如果我用这种方法生成第二个图,就不起作用。我应该如何绘制?以下是我所做的事情:

  1. import matplotlib.pyplot as plt
  2. def plot_graph():
  3. x = list(range(0,1000))
  4. y1 = [10*a-4 for a in x]
  5. y2 = [a**2 for a in x]
  6. plt.subplot2grid((2,2), (0,0))
  7. plt.plot(x,y1,y2)
  8. plt.xticks(list(range(0,120,20)), labels=[0,200,400,600,800,1000])
  9. plt.xlim(0,100)
  10. plt.yticks([1,100,1000,10000,100000])
  11. plt.ylim(1,100000)
  12. plt.grid(True)
  13. plt.plot(x,y1, "r-", label = "y=10.x-4")
  14. plt.plot(x,y2, "g-", label = "y=x²")
  15. plt.legend(loc = "lower right")
  16. plt.yscale('log')
  17. plt.xlabel('X')
  18. plt.ylabel('Y')
  19. plt.title('Functions:')
  20. plt.subplot2grid((2,2), (0,1))
  21. plt.plot(x,y1,y2)
  22. plt.grid(True)
  23. plt.plot(x,y1, "r-")
  24. plt.plot(x,y2, "g-")
  25. plt.yscale('log')
  26. plt.title('Intersection:')
  27. plot_graph()
英文:

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

如何重新创建一个gridspec subplot?

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:

  1. import matplotlib.pyplot as plt
  2. def plot_graph():
  3. x = list(range(0,1000))
  4. y1 = [10*a-4 for a in x]
  5. y2 = [a**2 for a in x]
  6. plt.subplot2grid((2,2), (0,0))
  7. plt.plot(x,y1,y2)
  8. plt.xticks(list(range(0,120,20)), labels=[0,200,400,600,800,1000])
  9. plt.xlim(0,100)
  10. plt.yticks([1,100,1000,10000,100000])
  11. plt.ylim(1,100000)
  12. plt.grid(True)
  13. plt.plot(x,y1, "r-", label = "y=10.x-4")
  14. plt.plot(x,y2, "g-", label = "y=x²")
  15. plt.legend(loc = "lower right")
  16. plt.yscale('log')
  17. plt.xlabel('X')
  18. plt.ylabel('Y')
  19. plt.title('Functions:')
  20. plt.subplot2grid((2,2), (0,1))
  21. plt.plot(x,y1,y2)
  22. plt.grid(True)
  23. plt.plot(x,y1, "r-")
  24. plt.plot(x,y2, "g-")
  25. plt.yscale('log')
  26. plt.title('Intersection:')
  27. plot_graph()

答案1

得分: 3

  1. 使用 plt.subplots 并带有关键字参数 width_ratio=[]
  2. 使用 ax.semilogy(x, y, ...) 进行绘图
  3. 在第二个子图中更改x和y的限制,然后您就可以进行缩放
英文:

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

如何重新创建一个gridspec subplot?

  1. from matplotlib.pyplot import show, subplots
  2. f, (a0, a1) = subplots(figsize=(10,2.5),
  3. ncols=2,
  4. width_ratios=[3, 1],
  5. layout='tight')
  6. x = [a/10 for a in range(0,1000)]
  7. y1 = [10*a-4 for a in x]
  8. y2 = [a**2 for a in x]
  9. a0.semilogy(x, y1, label='y1', lw=2, color='red')
  10. a0.semilogy(x, y2, label='y2', lw=2, color='green')
  11. a0.legend()
  12. a0.grid(1)
  13. a1.semilogy(x, y1, label='y1', lw=2, color='red')
  14. a1.semilogy(x, y2, label='y2', lw=2, color='green')
  15. a1.grid(1, 'both')
  16. a1.set_xlim((5, 15))
  17. a1.set_ylim((60, 150))
  18. show()

答案2

得分: 0

  1. import matplotlib.pyplot as plt
  2. def plot_graph():
  3. x = list(range(0,1000))
  4. y1 = [10*a-4 for a in x]
  5. y2 = [a**2 for a in x]
  6. plt.subplot2grid((2,2), (0,0))
  7. plt.plot(x,y1,'r-',label="y=10x-4")
  8. plt.plot(x,y2,'g-',label="y=x²")
  9. plt.xticks(list(range(0,400,100)), labels=[0,100,200,300])
  10. plt.xlim(0,300)
  11. plt.yticks([1,100,1000,10000,100000])
  12. plt.ylim(1,1000000)
  13. plt.grid(True)
  14. plt.legend(loc="lower right")
  15. plt.yscale('log')
  16. plt.xlabel('X')
  17. plt.ylabel('Y')
  18. plt.title('Functions:')
  19. plt.subplot2grid((2,2), (0,1))
  20. plt.plot(x,y1,'r-')
  21. plt.plot(x,y2,'g-')
  22. plt.grid(True)
  23. plt.xlim(0,20)
  24. plt.ylim(0.1,1000)
  25. plt.yscale('log')
  26. plt.title('Intersection:')
  27. plot_graph()
英文:
  1. import matplotlib.pyplot as plt
  2. def plot_graph():
  3. x = list(range(0,1000))
  4. y1 = [10*a-4 for a in x]
  5. y2 = [a**2 for a in x]
  6. plt.subplot2grid((2,2), (0,0))
  7. plt.plot(x,y1,y2)
  8. plt.xticks(list(range(0,400,100)), labels=[0,100,200,300])
  9. plt.xlim(0,300)
  10. plt.yticks([1,100,1000,10000,100000])
  11. plt.ylim(1,1000000)
  12. plt.grid(True)
  13. plt.plot(x,y1, "r-", label = "y=10.x-4")
  14. plt.plot(x,y2, "g-", label = "y=x²")
  15. plt.legend(loc = "lower right")
  16. plt.yscale('log')
  17. plt.xlabel('X')
  18. plt.ylabel('Y')
  19. plt.title('Functions:')
  20. plt.subplot2grid((2,2), (0,1))
  21. plt.plot(x,y1,y2)
  22. plt.grid(True)
  23. plt.plot(x,y1, "r-")
  24. plt.plot(x,y2, "g-")
  25. plt.xlim(0,20)
  26. plt.ylim(0.1,1000)
  27. plt.yscale('log')
  28. plt.title('Intersection:')
  29. plot_graph()

output:

如何重新创建一个gridspec subplot?

huangapple
  • 本文由 发表于 2023年5月6日 21:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定