I am working on a project and I need to plot multiple lines in one chart using function, I tried several times but I still don't know what is missing.
I did it without a function and it was successful but using the function is mandatory.
What you see is an artifact of using Matplotlib's "inline" backend in a notebook, the same code tested in a command line interpreter works as you intended, three lines and a single legend in a single Axes.
To remedy the problem is, fortunately, very easy: just dedent plt.show, so that it's executed once, when all the three lines have been placed in a single Axes.
def Plot(*cols):
for c in cols: plt.plot(...)
plt.legend() ; plt.show()
(note that also plt.legend can/should be outside of the loop).
Post Scriptum
If your function is meant to be executed in a "cell" of a Jupyter notebook, you can just omit completely the plt.show, because Jupyter will do that for you when you execute the cell.
Post Post Scriptum
The OP laments continuing problems, but I cannot reproduce their issues. To this aim, I have defined two functions, one with show outside the loop and another with show inside the loop, and at the bottom you can see what I get using the Jupyter notebook.
I whish good luck to the OP, I'm not going to help them further.
Related
I packaged some plotting functions that I frequently use across several jupyter notebooks. One of the functions makes a pdf and calls other plotting functions in the same package with a changed default parameter to prevent from showing the plot.
def plot_data(data, show_plot=True):
if not show_plot:
plt.ioff()
plt.plot(data)
plt.savefig()
def make_pdf():
plot_data(df, False)
# other code to generate pdf
When I call make_pdf() from the notebook, it changes show_plot and even if I pass show_plot=True afterwards it is not changing back and I get no plotting output. This is my first package and I am sure this has something to do with passing by reference vs passing by value, but I cannot figure out how to make it work the way I want to. I want to make the plot but not display it because I will save it locally, but don't want to see the plot when the pdf is being made.
How can I do this so that after I call make_pdf() I can call the other functions to show the plot again?
I am facing the following problem: I need two interactive plots to work at the same time on Jupyter, however they are interfering. When I rotate the cell of the first, the second plot stops being interactive and becomes "inline". Other times one of the plots looks like this:
The expected result was something like this
I imagine the problem is in the implementation I made. As you can see below, I use plt.something to put things in the figure (both for plot 1 and plot 2).
I'm using the %matplotlib notebook environment and tried to implement using fig1, ax1 = plt.subplots(). I would like to know if it is possible to do this type of implementation, where there is no conflict between plots? Maybe I am using matplotlib badly, so I would like some suggestions.
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.
Assume that for example I want to plot a function like f(x)=a*x**2 in which "a" is parameter, in this way.
First, I plot the function with a=2 and run my code. Second, I sweep "a" to 4 and run my code again. By default Pycharm will plot two figures in two windows but I want them two be in one figure with different colors. It is easy to do this in MATLAB but in python the only way that I know is to use two "matplotlib.pyplot.plot" commands in my code and define "a" before each, twice. How can I do this as I want?
I have been having some trouble with matplotlib since I started using python. When I use the interactive mode, I have to run ipython --pylab --wthread to get plots (if I don't use the --wthread command, it does not plot anything). So far, it hasn't been a problem.
Now, I want to:
Enter a loop
Plot something. This plot is a big plot with two subplots.
AFTER, show a button pannel with easygui to let the user decide depending on what he sees on the plot
close the plot
repeat for each thing to plot in my list.
I am finding several difficulties right now with this:
1) if I try to run the script in an interactive way using the run script.py command, then it does not plot annything, but directly jumps to the button pannel thing. However, if I stop the script, the plots appear. I am pretty sure that what is happening is that the "run" command does not show plots untill the script is done with the for loop. But I need the plots to appear before the buttons.
2) After some tries, I found this code to work (for some mystical reason to me...). I have tried other ways, but removing any of the show() or draw() commands just make the script not to work
fig=plt.figure(figsize=(16,8))
plt.show()
ax1=fig.add_subplot(121)
ax2.things...
ax2=fig.add_subplot(122)
ax2.things
plt.draw()
plt.show()
plt.draw()
showthebuttonsthing...
Even if this is working, matplotlib seems not to get along well with loops, and after 5 seconds of not pressing any button an just waiting, my matplotlib window gets grey. This might sound as something stupid, but colors are important for the decision I want the user the make...
3) I can't find a way to make a python script show plots if I run the python script outside ipython...
I guess there is something really wrong with my matplotlib configuration, but can't find a way to make this work...can anyone give me an hand on this?
Thanks a lot in advance!
It looks like you've almost got it, but try doing things in this order instead:
fig = plt.figure(figsize=(16,8))
ax = [fig.add_subplot(121),fig.add_subplot(122)]
ax[0].things
ax[1].things
plt.show()
#show the button panel
However, the best method may be to integrate the plot into your GUI. I am not familiar with EasyGUI, but it seems to be based on tk. This example should help you with embedding the figure into a tk window.