Issue
How can I have the line plot, with the data coming one by one? I have the code
import matplotlib.pyplot as plt
plt.ion()
plt.plot(1, 2)
plt.pause(2.5)
plt.plot(2, 5)
plt.pause(2.5)
but it does not show the line, if I add 'o' option in plot
function or change the plot
function to scatter
function, it works fine, But I want a line with no markers, how can I do this?
Solution
To respond to the comment about "deleting the current plot", and to improve upon Stefano's answer a bit, we can use Matplotlib's object-oriented interface to do this:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5, 6]
y = [3, 1, 4, 1, 5, 9, 2]
fig, ax = plt.subplots()
ax.set(xlim=(min(x), max(x)), ylim=(min(y), max(y)))
plt.ion()
(line,) = ax.plot([], []) # initially an empty line
timestep = 0.5 # in seconds
for i in range(1, len(x) + 1):
line.set_data(x[:i], y[:i])
plt.pause(timestep)
plt.show(block=True)
This version uses a single Line2D
object and modifies its underlying data via set_data
. Since Matplotlib doesn't have to draw multiple objects, just the one line, it should scale better once your data becomes large.
Answered By - Dominik StaĆczak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.