How to plot two functions into the same figure? - wolframalpha

I can plot this:
polar plot 6 e^(- ln(9/4)/(3pi) *a), a = 0..3/2 pi
and this:
plot 2/3 x - 4, x=0..6
But how to plot both into the same figure?

Related

for loop of scatterplots, setting the colorbar of a scatterplot horizontally under each plot

I am really new to plots and matplotlib
I am trying to plot several scatterplots with a loop for data from a pandas data frame
each scatterplot will have on the x axis data from the columns and on the y axis will be the data from the last column
I can make the plots work, but my color bar is displayed on the right side of each plot
I would like to set the color bar horizontally, under each plot but unfortunately I lack the necessary knowledge.
my code looks like this:
num_cols = df.columns.to_list()[:-1]
for col in num_cols:
df.plot.scatter(x = col, y = df.columns[-1], c=col, cmap='Paired', title=col, figsize = (5, 5))
current relust looks like this (sample of 1 plot from the loop):[current reslut]https://i.stack.imgur.com/m26wn.jpg
I tried a lines of code, but with no success.
I would like to have something like this (excuse my windows paint skills):expected result
You can use the cmap_location to move it to bottom.
num_cols = df.columns.to_list()[:-1]
for col in num_cols:
df.plot.scatter(x = col, y = df.columns[-1], c=col, cmap='Paired', title=col, figsize = (5, 5), cbar_location="bottom", cbar_mode="edge",
cbar_pad=0.25,
cbar_size="15%",)

Multiple different kinds of plots on a single figure and save it to a video

I am trying to plot multiple different plots on a single matplotlib figure with in a for loop. At the moment it is all good in matlab as shown in the picture below and then am able to save the figure as a video frame. Here is a link of a sample video generated in matlab for 10 frames
In python, tried it as below
import matplotlib.pyplot as plt
for frame in range(FrameStart,FrameEnd):#loop1
# data generation code within a for loop for n frames from source video
array1 = np.zeros((200, 3800))
array2 = np.zeros((19,2))
array3 = np.zeros((60,60))
for i in range(len(array2)):#loop2
#generate data for arrays 1 to 3 from the frame data
#end loop2
plt.subplot(6,1,1)
plt.imshow(DataArray,cmap='gray')
plt.subplot(6, 1, 2)
plt.bar(data2D[:,0], data2D[:,1])
plt.subplot(2, 2, 3)
plt.contourf(mapData)
# for fourth plot, use array2[3] and array2[5], plot it as shown and keep the\is #plot without erasing for next frame
not sure how to do the 4th axes with line plots. This needs to be there (done using hold on for this axis in matlab) for the entire sequence of frames processing in the for loop while the other 3 axes needs to be erased and updated with new data for each frame in the movie. The contour plot needs to be square all the time with color bar on the side. At the end of each frame processing, once all the axes are updated, it needs to be saved as a frame of a movie. Again this is easily done in matlab, but not sure in python.
Any suggestions
thanks
I guess you need something like this format.
I have used comments # in code to answer your queries. Please check the snippet
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(6,6))
ax1=fig.add_subplot(311) #3rows 1 column 1st plot
ax2=fig.add_subplot(312) #3rows 1 column 2nd plot
ax3=fig.add_subplot(325) #3rows 2 column 5th plot
ax4=fig.add_subplot(326) #3rows 2 column 6th plot
plt.show()
To turn off ticks you can use plt.axis('off'). I dont know how to interpolate your format so left it blank . You can adjust your figsize based on your requirements.
import numpy as np
from numpy import random
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(6,6)) #First is width Second is height
ax1=fig.add_subplot(311)
ax2=fig.add_subplot(312)
ax3=fig.add_subplot(325)
ax4=fig.add_subplot(326)
#Bar Plot
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
ax2.bar(langs,students)
#Contour Plot
xlist = np.linspace(-3.0, 3.0, 100)
ylist = np.linspace(-3.0, 3.0, 100)
X, Y = np.meshgrid(xlist, ylist)
Z = np.sqrt(X**2 + Y**2)
cp = ax3.contourf(X, Y, Z)
fig.colorbar(cp,ax=ax3) #Add a colorbar to a plot
#Multiple line plot
x = np.linspace(-1, 1, 50)
y1 = 2*x + 1
y2 = 2**x + 1
ax4.plot(x, y2)
ax4.plot(x, y1, color='red',linewidth=1.0)
plt.tight_layout() #Make sures plots dont overlap
plt.show()

matplotlib subplots do not show the exact x tick labels passed to it as list

I am plotting a plot of Accuracy versus the var_smoothing curve of 4 different instances. My values are:
var_smoothing_values
>>
[1e-09, 1e-06, 0.001, 1]
gauss_accuracies
>>
[0.728, 0.8, 0.826, 0.832]
I have used the 2 subplots and on the second subplot, I am plotting this as:
f,ax = plt.subplots(1,2,figsize=(15,5))
ax[1].plot(var_smoothing_values,gauss_accuracies,marker='*',markersize=12)
ax[1].set_ylabel('Accuracy')
ax[1].set_xlabel('var_smoothing values')
ax[1].set_title('Accuracy vs var_smoothing | GaussianNB',size='large')
plt.show()
ax[1].set_xticks(var_smoothing_values) shows only 3 ticks.
How can I show only 4 ticks which corresponds to each of my var_smoothing_values??
You need to use the log scale on the x-axis since your x-values span acoss several orders of magnitude
ax[1].set_xscale('log')
ax[1].set_xticks(var_smoothing_values);

How to plot in pandas - Different x and different y axis in a same plot

I want to plot different values of x and y-axis from different CSVs into a simple plot.
csv1:
Time Buff
1 5
2 10
3 15
csv2:
Time1 Buff1
2 3
4 6
5 9
I have 5 different CSVs. I tried plotting to concatenate the dataframes into a single frame and plot it. But I was able to plot with only one x-axis:
df = pd.read_csv('csv1.txt)
df1 = pd.read_csv('csv2.txt)
join = pd.concat([df, df1], axis=1)
join.plot(x='Time', y=['Buff', 'Buff1'], kind='line')
join.plot(x='Time', y='Buff', x='Time1', y='Buff1') #doesn't work
I end up getting a plot with reference with only one x-axis (csv1). But how to plot both x and y column from the CSVs into the same plot?
you can plot two dataframes in the same axis if you specify that axis with ax=. Notice that I created the figure and axis using subplots before i plotted either of the dataframes.
import pandas as pd
import matplotlib.pyplot as plt
f,ax = plt.subplots()
df = pd.DataFrame({'Time':[1,2,3],'Buff':[5,4,3]})
df1 = pd.DataFrame({'Time1':[2,3,4],'Buff1':[5,7,8]})
df.plot(x='Time',y='Buff',ax=ax)
df1.plot(x='Time1',y='Buff1',ax=ax)

python: python: scatter plot, The color of the Y axis makes the interval

I want to make intervals for each color, not to overlap on the Y axis.
How can I achieve this?
ax.set_title(df["me"][0],fontsize=20)
plt.xticks(rotation='vertical',fontsize=20)
plt.yticks(fontsize=20)
ax.set_ylabel(i,fontsize=40)
ax.grid(True)
plt.legend(loc='best',fontsize=20)
fig.savefig(i+".png")
plt.show()