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

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

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

问题

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

MWE

#-- 输入值
time = 3
tau_1 = 0.5
tau_2 = 0.02
#-- 绘图
fig = plt.figure()
plt.plot(time, tau_1, "--mo")
plt.plot(time, tau_2, "--co" )
plt.ylabel("tau", color="black", fontsize=14)
plt.xlabel("time", fontsize=14) 
plt.grid() 

#-- 在查看结果后手动更改输入

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

英文:

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

MWE

#-- Input values
time = 3
tau_1= 0.5
tau_2= 0.02
#-- Plot
fig = plt.figure()
plt.plot(time, tau_1, "--mo")
plt.plot(time, tau_2, "--co" )
plt.ylabel("tau",color="black",fontsize=14)
plt.xlabel("time", fontsize=14) 
plt.grid() 

#-- 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:

我之前在回答中误解了你的问题看起来你想要通过仅更改输入来多次运行相同的代码而不是在脚本中重新编写新的输入

一种方法是将绘制的图形保存到pickle文件中然后解除pickle以绘制新的输入

import os
import pickle
import matplotlib.pyplot as plt

#-- 输入值
time = 2
tau_1= 0.8
tau_2= 0.03

# 如果重新绘制,请使用保存的图形
if os.path.isfile('myplot.pickle'):
    with open('myplot.pickle', 'rb') as f:
        fig, ax = pickle.load(f)
        print('here')
else:  # 创建一个新的图形
    fig, ax = plt.subplots()

ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau", color="black", fontsize=14)
ax.set_xlabel("time", fontsize=14)
ax.grid()

# 将绘制的图形和轴保存到pickle文件中
with open('myplot.pickle', 'wb') as f:
    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.

import os
import pickle
import matplotlib.pyplot as plt

#-- Input values
time = 2
tau_1= 0.8
tau_2= 0.03

# If replotting, use the saved figure
if os.path.isfile('myplot.pickle'):
    with open('myplot.pickle', 'rb') as f:
        fig, ax = pickle.load(f)
        print('here')
else:  # Create a new one
    fig, ax = plt.subplots()

ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau",color="black",fontsize=14)
ax.set_xlabel("time", fontsize=14) 
ax.grid()

# Save the plotted figure and axes to a pickle file
with open('myplot.pickle', 'wb') as f:
    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

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

import matplotlib.pyplot as plt
#-- 输入值
time = 3
tau_1= 0.5
tau_2= 0.02
#-- 绘图
fig = plt.figure()
ax = plt.gca()  # 引用图表

ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau",color="black",fontsize=14)
ax.set_xlabel("time", fontsize=14) 
ax.grid() 

#-- 新输入,手动添加在查看结果后
time = 2
tau_1= 0.8
tau_2= 0.01

#-- 绘图
# 移除新图形的线
ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau",color="black",fontsize=14)
ax.set_xlabel("time", fontsize=14) 
ax.grid()
英文:

This is really simple: avoid creating a new figure

import matplotlib.pyplot as plt
#-- Input values
time = 3
tau_1= 0.5
tau_2= 0.02
#-- Plot
fig = plt.figure()
ax = plt.gca()  # reference to chart

ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau",color="black",fontsize=14)
ax.set_xlabel("time", fontsize=14) 
ax.grid() 

#-- New Input, added manually after seeing the results
time = 2
tau_1= 0.8
tau_2= 0.01

#-- Plot
# line with new figure removed
ax.plot(time, tau_1, "--mo")
ax.plot(time, tau_2, "--co" )
ax.set_ylabel("tau",color="black",fontsize=14)
ax.set_xlabel("time", fontsize=14) 
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:

确定