Julia displaying two plots on two seperate windows in a for loop - matplotlib

On Julia (version 1.8.3) in VsCode, I am running through a for loop and updating two plots simultaneously. However, the plot!() command only displays the last plot. An example program is
using Plots; pythonplot()
x = [1,2,3,4]
y = [8,7,6,4]
pt1 = plot()
pt2 = plot()
for i in 1:5
plot!(title="First Plot", pt1, x, i*y)
plot!(title="Second Plot", pt2, x, y/i)
end
display(pt1)
display(pt2)
The above program displays the second plot:
The same functionality happens if I try reuse='false' in both plot!() commands:
using Plots; pythonplot()
x = [1,2,3,4]
y = [8,7,6,4]
pt1 = plot()
pt2 = plot()
for i in 1:5
plot!(title="First Plot", pt1, x, i*y,reuse=false)
plot!(title="Second Plot", pt2, x, y/i,reuse=false)
end
display(pt1)
display(pt2)
I don't know if this is a specific problem with my Julia version (1.8.3) or with Windows 11. Shouldn't the separate plot!() commands display two different figures?

Related

How can I make a scatter Plot with what I have?

total_income_language = pd.DataFrame(df.groupby('movie_facebook_likes')['gross'].sum())
average_income_language = pd.DataFrame(df.groupby('movie_facebook_likes')['gross'].mean())
d = {'mean':'Average Income','sum':'Total Income'}
df1 = df.groupby('movie_facebook_likes')['gross'].agg(['sum','mean']).rename(columns=d)
ax = df1.plot.bar()
ax.set(xlabel='Facebook Likes', ylabel='Dollar Values(Gross)')
So, the code I have above does a good job ploting a bargraph. But When I tried to make it into a scatter plot by changing the .bar() to .scatter() It gives me the following error:
What do I need to fix to make it a scatter plot?
Expected Output:
As the error tells you, you need to specify x and y arguments which represent the coordinates of your points.
For example (assuming that there are movie_facebook_likes and gross columns in your DataFrame):
ax = df1.plot.scatter(x='movie_facebook_likes', y='gross')
See the documentation:
x, y : label or position, optional
Coordinates for each point.

Need correct x&y labels in for loop

I'm trying to create 21 scatter plots with data I have. These 21 plots have different combinations of data, and I have succeeded at creating the right plots. However, I cannot for the life of me figure out how to correctly label the plots. Here is my code:
F225W = np.loadtxt('path/phot_F225W.dat',usecols=[0], unpack=True)
F275W = np.loadtxt('path/phot_F275W.dat',usecols=[0], unpack=True)
... I did this for all filters
Filters = [F225W,F275W,F336W,F438W,F606W,F814W,F850L]
for i in range(len(Filters)):
for j in range(len(Filters)):
B = Filters[i]
R = Filters[j]
BR = B-R
if j<=i:
pass
else:
plt.figure()
plt.gca().invert_yaxis()
plt.xlim(-6,6)
plt.ylim(-4,-15)
plt.xlabel(str(Filters[i]) + '-' + str(Filters[j]))
plt.ylabel(str(Filters[j]))
plt.plot(BR,R,'k.',markersize=1)
plt.show()
The code is supposed to iterate through the different combinations of filters and plot B-R vs. R, but instead of just labeling it B-R and R, I need it to show me the filters that were used in creating the plot. At the moment it creates the correct plots, but the labels don't show up.
To expand on the comment, does this work as a solution? The loop will pause until you close each figure that pops up on each iteration (if you keep the plt.show() ). You could alternatively save each figure and look at them separately as indicated in the solution as well:
Filters = [F225W,F275W,F336W,F438W,F606W,F814W,F850L]
Filter_names = ['F225W','F275W','F336W','F438W','F606W','F814W','F850L']
for i in range(len(Filters)):
for j in range(len(Filters)):
B = Filters[i]
BB = Filter_names[i]
R = Filters[j]
RR = Filter_names[j]
BR = B-R
if j<=i:
pass
else:
plt.figure()
plt.gca().invert_yaxis()
plt.xlim(-6,6)
plt.ylim(-4,-15)
plt.xlabel(str(Filter_names[i]) + '-' + str(Filter_names[j]))
plt.ylabel(str(Filter_names[j]))
plt.title('B filter:' + BB + '\tR Filter:' + RR)
plt.plot(BR,R,'k.',markersize=1)
os.chdir(path_you_want_to_save_to)
plt.savefig('B_' + BB +'_R_' + RR + '.png')
#uncomment line to see graph and pause loop.
#also note the indentation has changed
#plt.show()
plt.close()
After looking again I'm guessing the Filters are lists of arrays of some sort? So you need another list Filter_names for the string representing their names. I think that fixes your problem as you were trying to label them with list data before.

Plot sphere with Julia and PyPlot

Recently I tried to plot a sphere using PyPlot/Julia and unfortunately it was harder than I thought.
Probably there's something wrong with points generation, but I can't figure out why my implementation didn't work. Although everything is fine with original python code.
I've tried to adapt demo2 from matplotlib surface plot doc as MWE:
using PyPlot
u = linspace(0,2*π,100);
v = linspace(0,π,100);
x = cos(u).*sin(v);
y = sin(u).*sin(v);
z = cos(v);
surf(x,y,z)
And I'm getting instead of
So, what's exactly wrong in my Julia implementation?
x, y and z should be matrices, not vectors -- otherwise you only have a curve drawn on the sphere, instead of the surface itself.
using PyPlot
n = 100
u = linspace(0,2*π,n);
v = linspace(0,π,n);
x = cos(u) * sin(v)';
y = sin(u) * sin(v)';
z = ones(n) * cos(v)';
# The rstride and cstride arguments default to 10
surf(x,y,z, rstride=4, cstride=4)
The curve initially drawn corresponds to the diagonal of those matrices.
plot( diag(x), diag(y), diag(z), color="yellow", linewidth=3 )
This no longer works in Julia 1.1.2 to draw the sphere. Use this instead
using PyPlot
n = 100
u = range(0,stop=2*π,length=n);
v = range(0,stop=π,length=n);
x = cos.(u) * sin.(v)';
y = sin.(u) * sin.(v)';
z = ones(n) * cos.(v)';
# The rstride and cstride arguments default to 10
surf(x,y,z, rstride=4, cstride=4)

Matplotlib: 'savefig' throw TypeError when 'linewidths' property is set

When the 'linewidths' property is set, calling 'savefig' throws 'TypeError: cannot perform reduce with flexible type'. Here is a MWE:
# Create sample data.
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-2.0, 2.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = 10.0 * (2*X - Y)
# Plot sample data.
plt.contour(X, Y, Z, colors = 'black', linewidths = '1')
plt.savefig('test.pdf')
It is not a problem with the figure rendering (calling 'plt.show()' works fine). If the linewidths property is not set, e.g. changing the second last line to
plt.contour(X, Y, Z, colors = 'black')
'savefig' works as intended. Is this a bug or have i missed something?
This is not a bug, since the documentation for plt.contour() specifies that linewidths should be a [ None | number | tuple of numbers ] while you provide a number as a string.
Here is my output with your code (I am using matplotlib 1.4.3).
>>> matplotlib.__version__
'1.4.3'
Your code 'works' under Python 2.7 but the linewidths parameter is effectively ignored, producing plots that look like this, regardless of the value (this was with linewidths='10'.
In contrast on Python 3.4 I get the following error:
TypeError: unorderable types: str() > int()
Setting linewidths to an int (or a float) as follows produces the correct output and works on both Python 2.7 and Python 3.4. Again, this is with it set to 10:
plt.contour(X, Y, Z, colors = 'black', linewidths = 10)

How can I use Matplotlib with different axes

Does anyone know how can i draw more lines with matplotlib.pyplot.plot but forcing them to use its own axes?
eg I have data in lists a b c
a is the base of the others (time), so I would like to draw how b and c changes
but b contains big numbers and c contains small numbers, so when i draw both then i can see only b
thanks
You just have to add a secondary axis to your plot.
As an example, this code...
from matplotlib.pyplot import *
#creating some data
a = range(10)
b = [2*x for x in a]
c = [x**10 for x in a]
fig = figure()
ax1 = fig.add_subplot(111)
ax1.set_ylabel('$y = 2 . x$')
ax1.plot(a, b, 'yo')
ax2 = ax1.twinx() #create a twin of Axes for generating a plot
# with a share x-axis but independent y axis
ax2.set_ylabel('$y = x^{10}$')
ax2.plot(a,c,'b-')
show()
...will generate this figure: