I`m using matplotlib to plot a reward vs episode plot in a reinforcement learning application. So basically I update the plot every time the episode ends.
Searching here python-How do I plot in real time in a while loop using matplotlib I get to make my plot update every time my episode finished. My code for the plot is:
reward_plot = plt.plot(i,total_reward, '-ro')
plt.title('Reward vs episodes')
plt.xlabel("Episodes", fontsize=12)
plt.ylabel("Reward", fontsize=12)
plt.pause(0.05)
And the plot, actually looks like this:
I just want to make the plot draw a line linking the dots each time the graph is updated. It is possible? I tried with different methods but I didn't have any luck. I'm using Python 3.6.6.
Related
I need write a script to plot a curve in python with continuously changing colors. I need to use the command ‘fig, ax = plt.subplots()’ and a for loop. I have not tried anything as I am not sure what to put in the for loop.
for debugging purposes I would like to have my script show intermediate steps and not just the final plot when working with matplotlib. The problem is that after calling plt.show(), the axis is reset and drawing starts anew. Is there a way to call plt.show() and continue working with the same axis?
I am working inside PyCharm.
Code concept:
import statements
create part of plot
plt.show()
create next part of plot
plt.show() # Should show whatever was in the first plotted window plus what was added in the meantime
create final part of plot
plt.show() # Should show whatever was in the second plotted window plus what was added in the meantime
Thanks!
Edit:
System: Windows 10 running Python 3.7.1 and matplotlib 3.3.3
Your best bet to save the plot as it evolves and then compare the different saved figures.
#create part of plot
plt.savefig('first_part')
#create rest of plot
plt.savefig('second_part')
I have been using the same setup for quite some time now but suddenly I am no longer allowed to plot more than one graph in a program.
Usually I can plot multiple plots after each other and let the program run through it. It executes the next lines of code after closing the first window. However, recently the first plot is not shown but instead the data is added to the last plot.
I have included a sample code which used to give me two plots but now only one.
import matplotlib.pyplot as plt
import numpy as np
random_num = np.random.randint(0,5,10)
random_num_2 = np.random.randint(0,100,10)
plt.plot(random_num, 'ko')
plt.show()
plt.plot(random_num_2, 'g*')
plt.show()
The first image shows the output from my program. But I would like to have them separated into two plots like Figure 2 and 3 show.
Maybe I should add that I am using Python 3.6 with Spyder 3.2.4. The graphics option is set to display it in Qt5 even though I tried all settings and only 'Inline' shows me the results the way I want it.
Sorry if this is a very simple question. I have tried googling but I only come up with questions about my topic where the way mine works would be the solution not the problem.
#TheresaOtt. I would suggest you create a new figure instance (plt.figure()) for each plot and use only once at the end the plt.show() command.
In my plotting application written in Python, I use matplotlib (with TkAgg backend) to draw some data.
This plot is interactive, so the user can drag/zoom the plot region. However, sometimes the application needs to clear the curves and plot them anew - and as a result, the plot rescales to the extent of the curves.
It would be better to keep the user-defined limits. However, calling e.g. ax.get_xlim() returns the limits that were originally set by ax.set_xlim(...), not the current limits that the user has changed by their mouse.
I tried hard to find the solution in the documentation and examples, yet still I could not resolve this simple task: How to get the current limits of interactive matplotlib plot?
Is there any (simple or complex) way to recreate this plot in matplotlib?
I've tried plotting it using a scatter plot with two different x-values, while adding a small random number to it, but obviously it didn't produce the nice "ordered" effect seen above.
There's a package built on top of matplotlib called beeswarm that positions the points as requested.