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?
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 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.
So I have this function,
func(a,x,y)
where x,y are the graphs coordinates, and a some other data (it doesnt really matter what a is for this problem)
I want this function to make subplots automatically,
so when the function is called and n number of times
func(a2,x2,y2)
func(a3,x3,y3)
func(a4,x4,y4)
.
.
func(an,xn,yn)
It should show n subplots on a graph
Note it is important that the function works in this way for my usecase, I dont want to be inputting an array of [a1,a2,...],[x1,x2,...],[y1,y2,...] and then plotting them all, I want to call each of the graph function seperately
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 a project I'm working on I have to plot a huge amount of lines into one figure. Therefore I am using a for loop like this:
for i in xrange(0,len(x)):
ax[plotId].plot(x[i],y[i],color = usedColors[i])
Unfortunately, this loop needs around 0.8 seconds which is to much for me since I use matplotlib in an interactive way (the user is able to change the plot with integrated buttons. Is there an alternative way to do this? For example write all the related data in a list while loading the GUI:
l = [x[0],y[0],usedColors[0],x[1],x[2]....]
and use this list somehow to write the data in plot() without using a loop (which is not working because ax[plotId].plot cannot interpret this list in a proper way):
ax[plotId].plot(l)
Any ideas?
Update:
I was looking for LineCollection