如何在Python图表中手动绘制点(Matlab中的hold on函数)

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

How to plot manually points in Python charts (hold on function in Matlab)

问题

我想在Python图表中手动绘制点。这应该是Matlab中的hold on功能。

MWE

  1. #-- 输入值
  2. time = 3
  3. tau_1 = 0.5
  4. tau_2 = 0.02
  5. #-- 绘图
  6. fig = plt.figure()
  7. plt.plot(time, tau_1, "--mo")
  8. plt.plot(time, tau_2, "--co" )
  9. plt.ylabel("tau", color="black", fontsize=14)
  10. plt.xlabel("time", fontsize=14)
  11. plt.grid()
  12. #-- 在查看结果后手动更改输入

Python应该在同一张图中绘制输入值,在每次手动更改输入值后。我想强调我特别希望这种解决方案,用户运行程序,并手动更改输入值。

英文:

I want to plot manually points in python charts. This should be the hold on function in Matlab.

MWE

  1. #-- Input values
  2. time = 3
  3. tau_1= 0.5
  4. tau_2= 0.02
  5. #-- Plot
  6. fig = plt.figure()
  7. plt.plot(time, tau_1, "--mo")
  8. plt.plot(time, tau_2, "--co" )
  9. plt.ylabel("tau",color="black",fontsize=14)
  10. plt.xlabel("time", fontsize=14)
  11. plt.grid()
  12. #-- Change the Input manually after seeing the results

Python should plot in the same chart the input values at each manual change of the input. I want to underline that I specifically want this solution, where the user run the program, and change manually the input values

答案1

得分: 1

Here's the translated code portion:

  1. 我之前在回答中误解了你的问题看起来你想要通过仅更改输入来多次运行相同的代码而不是在脚本中重新编写新的输入
  2. 一种方法是将绘制的图形保存到pickle文件中然后解除pickle以绘制新的输入
  3. import os
  4. import pickle
  5. import matplotlib.pyplot as plt
  6. #-- 输入值
  7. time = 2
  8. tau_1= 0.8
  9. tau_2= 0.03
  10. # 如果重新绘制,请使用保存的图形
  11. if os.path.isfile('myplot.pickle'):
  12. with open('myplot.pickle', 'rb') as f:
  13. fig, ax = pickle.load(f)
  14. print('here')
  15. else: # 创建一个新的图形
  16. fig, ax = plt.subplots()
  17. ax.plot(time, tau_1, "--mo")
  18. ax.plot(time, tau_2, "--co" )
  19. ax.set_ylabel("tau", color="black", fontsize=14)
  20. ax.set_xlabel("time", fontsize=14)
  21. ax.grid()
  22. # 将绘制的图形和轴保存到pickle文件中
  23. with open('myplot.pickle', 'wb') as f:
  24. pickle.dump((fig, ax), f)

请注意,这只是代码的翻译部分,不包括问题或其他内容。

英文:

EDIT: I misunderstood your question in my earlier answer. It looks like you want to run the same code over and over by just changing the inputs, not rewrite the new inputs again in the script.

One approach is to save the plotted figure to a pickle file. Then unpickle it to plot the new inputs.

  1. import os
  2. import pickle
  3. import matplotlib.pyplot as plt
  4. #-- Input values
  5. time = 2
  6. tau_1= 0.8
  7. tau_2= 0.03
  8. # If replotting, use the saved figure
  9. if os.path.isfile('myplot.pickle'):
  10. with open('myplot.pickle', 'rb') as f:
  11. fig, ax = pickle.load(f)
  12. print('here')
  13. else: # Create a new one
  14. fig, ax = plt.subplots()
  15. ax.plot(time, tau_1, "--mo")
  16. ax.plot(time, tau_2, "--co" )
  17. ax.set_ylabel("tau",color="black",fontsize=14)
  18. ax.set_xlabel("time", fontsize=14)
  19. ax.grid()
  20. # Save the plotted figure and axes to a pickle file
  21. with open('myplot.pickle', 'wb') as f:
  22. pickle.dump((fig, ax), f)

Keep changing the inputs values on top and run it again. It will plot to the same figure.

如何在Python图表中手动绘制点(Matlab中的hold on函数)

答案2

得分: 0

这很简单:避免创建新图形

  1. import matplotlib.pyplot as plt
  2. #-- 输入值
  3. time = 3
  4. tau_1= 0.5
  5. tau_2= 0.02
  6. #-- 绘图
  7. fig = plt.figure()
  8. ax = plt.gca() # 引用图表
  9. ax.plot(time, tau_1, "--mo")
  10. ax.plot(time, tau_2, "--co" )
  11. ax.set_ylabel("tau",color="black",fontsize=14)
  12. ax.set_xlabel("time", fontsize=14)
  13. ax.grid()
  14. #-- 新输入,手动添加在查看结果后
  15. time = 2
  16. tau_1= 0.8
  17. tau_2= 0.01
  18. #-- 绘图
  19. # 移除新图形的线
  20. ax.plot(time, tau_1, "--mo")
  21. ax.plot(time, tau_2, "--co" )
  22. ax.set_ylabel("tau",color="black",fontsize=14)
  23. ax.set_xlabel("time", fontsize=14)
  24. ax.grid()
英文:

This is really simple: avoid creating a new figure

  1. import matplotlib.pyplot as plt
  2. #-- Input values
  3. time = 3
  4. tau_1= 0.5
  5. tau_2= 0.02
  6. #-- Plot
  7. fig = plt.figure()
  8. ax = plt.gca() # reference to chart
  9. ax.plot(time, tau_1, "--mo")
  10. ax.plot(time, tau_2, "--co" )
  11. ax.set_ylabel("tau",color="black",fontsize=14)
  12. ax.set_xlabel("time", fontsize=14)
  13. ax.grid()
  14. #-- New Input, added manually after seeing the results
  15. time = 2
  16. tau_1= 0.8
  17. tau_2= 0.01
  18. #-- Plot
  19. # line with new figure removed
  20. ax.plot(time, tau_1, "--mo")
  21. ax.plot(time, tau_2, "--co" )
  22. ax.set_ylabel("tau",color="black",fontsize=14)
  23. ax.set_xlabel("time", fontsize=14)
  24. ax.grid()

huangapple
  • 本文由 发表于 2023年5月24日 18:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322585.html
匿名

发表评论

匿名网友

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

确定