Im calculating the flow in a lid-driven cavity, and Im plotting the result with a quiver. I want to save the plot in every time step, but obviously, as the name is the same, it´s only keep the last one, how can I do it?
for n in range(nt):
#Here I do all the calculation to obtain the new u and v
uC=0.5*(u[:,1:] + u[:,:-1])
vC=0.5*(v[1:,:] + v[:-1,:])
plt.cla()
plt.quiver(x, y, uC, vC);
plt.draw()
plt.savefig( "Instant1.png")
So, imagine nt = 10, I want to get ten differents png files. Any ideas?
I aprecciate all your help
You could change the file name each time:
plt.savefig("Instant{}.png".format(n))
Also, If you have more than ten plots, it might be a good idea to have some leading zeroes, eg. so "Instant5.png" doesn't come after "Instant10.png" in lexicographic order.
plt.savefig("Instant{:03}.png".format(n))
You could also do:
plt.savefig("Instand"+str(n)+".png")
Related
I'm trying to represent three different data sets on the same histogram but one is 100 data points, one is 362 and one is 289. I'd like to scale the latter two by factors of 3.62 and 2.89 respectively so they don't overshadow the 100 point one. I feel like this should be easy but I'm not sure where to put my division. I feel like I've tried all the spots you can try. Here's how it is now:
plt.figure(figsize=(10,6))
scale_pc = (1 / 3.62) #this is the math I'd like to use, but where to put it?
scale_ar = (1 / 2.89) #this is the math I'd like to use, but where to put it?
alldf2[alldf2['playlist']==1]['danceability'].hist(bins=35, color='orange', label='PopConnoisseur', alpha=0.6)
alldf2[alldf2['playlist']==2]['danceability'].hist(bins=35, color='green',label='Ambient',alpha=0.6)
alldf2[alldf2['playlist']==0]['danceability'].hist(bins=35, color='blue',label='Billboard',alpha=0.6)
plt.legend()
plt.xlabel('Danceability')
I've tried variations on this but none work:
alldf2[alldf2['playlist']==1]['danceability'].hist(bins=35, color='orange', label='PopConnoisseur', alpha=0.6)
alldf2[alldf2['playlist']==2]['danceability'/ 3.62].hist(bins=35, color='green',label='Ambient',alpha=0.6)
alldf2[alldf2['playlist']==0]['danceability'/ 2.89].hist(bins=35, color='blue',label='Billboard',alpha=0.6)
Any thoughts?
Edit: Here's the plot as it currently is:
The 2nd one for sure won't work because it seems to have a syntax error here
'danceability'/ 3.62
in parenthesis you are calling the column I do not think that you can divide the values like that. Moreover, even if something like that would work it would probably divide your values in that column by 3.62, not return 100 data points...
Also I am not sure what is the problem with having more data points in the other histogram, that's kind of the thing which you want the histogram to show - i.e. how many elements are having a particular value.
Also, as Blazej said in the comment, give an example of data so we can understand a bit more what are you trying to do. Specify what you want to achieve by using just 100 points.
There is a for-loop in my part of code, and every step it can generate new tpr(as X), fpr(as Y) like that
0.05263157894736842 0.1896551724137931
0.06578947368421052 0.19540229885057472
0.07894736842105263 0.22988505747126436
0.07894736842105263 0.25862068965517243
0.07894736842105263 0.28735632183908044
I want collect all these points and get a full plot, but it didn't work. And my code are attached below
for i in range (-30,20):
predicted = (np.sign(t+i*1e-4)+1)/2.
vals, cm = re.get_CM_vals(y_test, predicted)
tpr = re.TPR_CM(cm)
fpr = re.FPR_CM(cm)
#print(tpr, fpr)
plt.plot(fpr, tpr,'b.-',linewidth=1)
plt.show()
Beside, I want to the the right angle line between points like that.is there a func in matplotlib?
Using your current code, I suggest adding the x values to an array and the y values to another array. You could also use something like: ArrayName = [[],[]], then append the x and y values to ArrayName[0] and ArrayName[1], respectively. Not only would this actually work, but it would be slightly faster, since the plt.plot and plt.scatter functions work faster plotting all the points at once instead of through a for loop.
If you don't want to plot the points connected with lines, I still suggest using an array since that would be faster. (It wouldn't be that much faster in this case, but it's a good habit to have.
I have written a complicated code. The code produces a set of numbers which I want to plot them. The problem is that I cannot put those numbers in a list since there are 2 700 000 000 of them.
So I need to plot one point then produce second point (the first point is replaced by second point so the first one is erased because I cannot store them). These numbers are generated in different sections of the code so I need to hold (MATLAB code) the figure.
For making it more conceivable to you, I write a simple code here and I want you to show me how to plot it.
import matplotlib.pyplot as plt
i=0
j=10
while i<2700000000:
plt.stem(i, j, '-')
i = i + 1
j = j + 2
plt.show()
Suppose I have billions of i and j!
Hmm I'm not sure if I understood you correctly but this:
import matplotlib.pyplot as plt
i=0
j=10
fig=plt.figure()
ax=fig.gca()
while i<10000: # Fewer points for speed.
ax.stem([i], [j]) # Need to provide iterable arguments to ax.stem
i = i + 1
j = j + 2
fig.show()
generates the following figure:
Isn't this what you're trying to achieve? After all the input numbers aren't stored anywhere, just added to the figure as soon as they are generated. You don't really need Matlab's hold equivalent, the figure won't be shown until you call fig.show() or plt.show() to show the current figure.
Or are you trying to overcome the problem that you can' hold the matplotlib.figure in your RAM? In which case my answer doesn't answer your question. Then you either have to save partial figures (only parts of the data) as pictures and combine them, as suggested in the comments, or think about an alternative way to show the data, as suggested in the other answer.
I have a script that makes a Venn diagram of 3 sets using the matplotlib_venn module, as follows:
union = set_1.union(set_2).union(set_3)
indicators = ['%d%d%d' % (a in set_1, a in set_2, a in set_3) for a in union]
subsets = Counter(indicators)
fig = plt.figure((n+1)*2 - 1)
ax = fig.add_subplot(1, 1, 1)
v = venn3(subsets, (compare[0], compare[1], compare[2]), ax=ax)
plt.show()
Here are two images I get out of this, from two different datasets with three sets each (one small and one large):
In this image, The numbers are off. 180 should be in the middle and the 2 should be somewhere on the right side of the image, at the barely visible yellow/green part. I first thought this was due to the small size of the data set, but looking at the larger set ...
... I can still see that the numbers are slightly off, although not as much as previously. The larger, common set is still not in the middle, and the other numbers seem to be a little off to where the "center" of their set is.
Any ideas as to why this is, and what can be done to remedy the problem?
Using the venn3_unweighted function rather than venn3 shows perfectly nice (an non-proportional) images, including any 0s in the smaller data set, but it just doesn't work with the proportional version.
I have this program which will solve a system of differential equations in such way that two values xi, yi are produced each time a for loop iterates. Is there some way i could plot these values as they are produced instead of waiting for the computation to finish, then plotting them all?
You might want to take a look at matplotlib (http://matplotlib.sourceforge.net/) or gnuplot.py (http://gnuplot-py.sourceforge.net/). I've used these to plot data after I was done, but I don't see a reason why you could not do this while you are generating the data. However, depending on how fast your loop executes, chances are this will slow down your whole program.
The gnuplot may not be maintained any longer, but I have been using it w/o any problems.
Let's say your algorithm looks like this:
import matplotlib
x_list = []
y_list = []
while True:
xi, yi = compute() # calculates your xi and yi value
if converged(xi, yi):
break
# capture history of your xi and yi
x_list.append(xi)
y_list.append(yi)
matplotlib.pyplot.plot(x, y) # create a graph representing x and y