Matplotlib保存图形时互相覆盖

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

Matplotlib Save Figures Write Over Each Other

问题

这是你提供的代码的翻译部分:

  1. 我有两个函数用于创建保存的图表但第二个图表总是覆盖在第一个图表上 - 这是用于重现问题的代码在这种情况下graph.png 将是 dendo.png 和本来应该是单独的 graph.png 的组合这只是 Matplotlib 的工作方式还是我遗漏了什么
  2. def dendo():
  3. # 创建一个数组
  4. x = np.array([100., 200., 300., 400., 500., 250.,
  5. 450., 280., 450., 750.])
  6. Z = linkage(x, 'ward')
  7. dendrogram(Z, leaf_rotation=45., leaf_font_size=12.)
  8. plt.title('层次聚类树状图')
  9. plt.xlabel('聚类大小')
  10. plt.ylabel('距离')
  11. plt.rcParams["figure.figsize"] = (9, 8)
  12. plt.savefig('dendo.png')
  13. def stats():
  14. df = pd.DataFrame({'gmat': [580.0, 660.0, 740.0, 590.0, 660.0, 540.0, 690.0, 550.0, 580.0, 620.0, 710.0, 660.0, 780.0, 680.0, 680.0, 550.0, 580.0, 650.0, 620.0, 750.0, 730.0, 690.0, 570.0, 660.0, 690.0, 650.0, 670.0, 690.0, 690.0, 590.0],
  15. 'gpa': [2.7, 3.3, 3.3, 1.7, 4.0, 2.7, 2.3, 2.7, 2.3, 2.7, 3.7, 3.3, 4.0, 3.3, 3.9, 2.3, 3.3, 3.7, 3.3, 3.9, 3.7, 1.7, 3.0, 3.7, 3.3, 3.7, 3.3, 3.7, 3.7, 2.3],
  16. '工作经验': [4.0, 6.0, 5.0, 4.0, 4.0, 2.0, 1.0, 1.0, 2.0, 2.0, 5.0, 5.0, 3.0, 4.0, 4.0, 4.0, 1.0, 6.0, 2.0, 4.0, 6.0, 1.0, 2.0, 4.0, 3.0, 6.0, 6.0, 5.0, 5.0, 3.0],
  17. '录取': [0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]})
  18. Xtrain = df[['gmat', 'gpa', '工作经验']]
  19. ytrain = df[['录取']]
  20. log_reg = sm.Logit(ytrain, Xtrain).fit()
  21. stats1 = log_reg.summary()
  22. plt.rc('figure', figsize=(12, 7))
  23. plt.text(0.01, 0.05, str(stats1), {'fontsize': 10}, fontproperties='等宽')
  24. plt.axis('off')
  25. plt.tight_layout()
  26. plt.savefig('graph.png')
  27. dendo()
  28. stats()
英文:

I have two functions that create saved graphs, but the second graph always has the first graph laid over it - here is the code to reproduce the problem. In this case graph.png will be a combination of dendo.png and what was supposed to be graph.png by itself. Is this just way Matplotlib works or is there something I'm missing?

  1. def dendo():
  2. # Create an array
  3. x = np.array([100., 200., 300., 400., 500., 250.,
  4. 450., 280., 450., 750.])
  5. Z=linkage(x,'ward')
  6. dendrogram(Z, leaf_rotation=45., leaf_font_size=12.)
  7. plt.title('Hierarchical Clustering Dendrogram')
  8. plt.xlabel('Cluster Size')
  9. plt.ylabel('Distance')
  10. plt.rcParams["figure.figsize"] = (9,8)
  11. plt.savefig('dendo.png')
  12. def stats():
  13. df = pd.DataFrame({'gmat':[ 580.0, 660.0, 740.0, 590.0, 660.0, 540.0, 690.0, 550.0, 580.0, 620.0, 710.0, 660.0, 780.0, 680.0, 680.0, 550.0, 580.0, 650.0, 620.0, 750.0, 730.0, 690.0, 570.0, 660.0, 690.0, 650.0, 670.0, 690.0, 690.0, 590.0],
  14. 'gpa': [2.7, 3.3, 3.3, 1.7, 4.0, 2.7, 2.3, 2.7, 2.3, 2.7, 3.7, 3.3, 4.0, 3.3, 3.9, 2.3, 3.3, 3.7, 3.3, 3.9, 3.7, 1.7, 3.0, 3.7, 3.3, 3.7, 3.3, 3.7, 3.7, 2.3],
  15. 'work_experience': [4.0, 6.0, 5.0, 4.0, 4.0, 2.0, 1.0, 1.0, 2.0, 2.0, 5.0, 5.0, 3.0, 4.0, 4.0, 4.0, 1.0, 6.0, 2.0, 4.0, 6.0, 1.0, 2.0, 4.0, 3.0, 6.0, 6.0, 5.0, 5.0, 3.0],
  16. 'admitted': [0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]})
  17. Xtrain = df[['gmat', 'gpa', 'work_experience']]
  18. ytrain = df[['admitted']]
  19. log_reg = sm.Logit(ytrain, Xtrain).fit()
  20. stats1=log_reg.summary()
  21. plt.rc('figure', figsize=(12, 7))
  22. plt.text(0.01, 0.05, str(stats1), {'fontsize': 10}, fontproperties = 'monospace')
  23. plt.axis('off')
  24. plt.tight_layout()
  25. plt.savefig('graph.png')
  26. dendo()
  27. stats()

答案1

得分: 2

以下是翻译好的部分:

  1. 使用 plt.figure 在绘制新图之前:
  1. # 第一个图
  2. plt.figure(0)
  3. plt.plot(range(10))
  4. plt.savefig('0.png')
  5. # 第二个图
  6. plt.figure(1)
  7. plt.plot(sorted(range(10), reverse = True))
  8. plt.savefig('1.png')
  1. 使用 plt.subplots
  1. # 第一个图
  2. fig1, ax1 = plt.subplots()
  3. ax1.plot(range(10))
  4. fig1.savefig('1.png')
  5. # 第二个图
  6. fig2, ax2 = plt.subplots()
  7. ax2.plot(sorted(range(10), reverse = True))
  8. fig2.savefig('2.png')

更多详情请参阅文档或matplotlib示例。

英文:

There are two ways (can't use your code since it's not reproducible):

  1. Use plt.figure before drawing a new figure:
  1. # 1st figure
  2. plt.figure(0)
  3. plt.plot(range(10))
  4. plt.savefig('0.png')
  5. # 2nd figure
  6. plt.figure(1)
  7. plt.plot(sorted(range(10), reverse = True))
  8. plt.savefig('1.png')
  1. Use plt.subplots:
  1. # 1st figure
  2. fig1, ax1 = plt.subplots()
  3. ax1.plot(range(10))
  4. fig1.savefig('1.png')
  5. # 2nd figure
  6. fig2, ax2 = plt.subplots()
  7. ax2.plot(sorted(range(10), reverse = True))
  8. fig2.savefig('2.png')

For more details see documentation or matplotlib examples.

huangapple
  • 本文由 发表于 2023年3月4日 07:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632773.html
匿名

发表评论

匿名网友

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

确定