Plot from Matplotlib and Pandas on the same figure - python

I need two plots on the same figure. One of them is described by a Dataframe and the other by numpy arrays. Is there a way to plot them on the same figure without converting any of them ?
I know how to make multiple plots if all are numpy arrays or if all are Dataframes, but I don't know what to do when they have mixed types. For example, the following does not work:
ax=plt.plot(xv,yv)
df.plot.scatter(x='Column1',y='Column2',ax=ax)

If you want two plots on the same figure:
fig, (ax1,ax2) = plt.subplots(2)
ax1.plot(xv,yv)
df.plot.scatter(x='Column1',y='Column2',ax=ax2)

Related

Show plots side by side

I have functions that produces plots,
And i used it three time with different values to give me three plots.
My question is that i want to put the plots side by side horizontally to be able to compare them.
As doing the following show every plot after the other vertically.
make_plot(twiss)
make_plot(twiss_error)
make_plot(twiss_corrected)
If you are using matplotlib perhaps use subplots:
https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.subplots.html
import matplotlib.pyplot as plt
fig, ax = plt.subplots (1,2)
you can access subplots by index as in
ax[0]
in your code.
Hope that this helps.

How can I plot figures from pandas series in different windows?

I am new in python and I create pandas series in a for-loop. Each time I want to plot the series in a figure. I use ax = series.plot(title = str(i)+'.jpg') but all figures are plotted in same window. How can I plot them in different windows?
If you are using matplotlib, use
plt.figure()
for every new figure you want to plot.
You then show all figures with
plt.show()
also, you should check out what subplots does.

Superimposing some plots with a txt file

`I am trying to reproduce the attached figure step by step. My problem was that how can i plot colorbar in above figure by my data. My data is a cosmological data and it has 7 columns totally with many raw. My main goal is reproducing the present figure step by step. You can see that there are three different plots which are interpolated each other. Firstly, i tried to plot small colorful lines in the body of figure by using two columns of data. I did it by scatter plots and then i needed to reproduce the colorbar part of figure. But, it was not possible at the first attempt. Because, the colorbar points was not a part of data. Then, i obtained the values of colorbar by some calculations and added them as additional columns to data. Now, i could you the simple colorbar function to do colorbar part. And i got it. For the next step, i need to turn small curved lines to dark solid lines.
How can I do plots in matplotlib?
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
data1 = np.loadtxt("bei_predic.txt", unpack=True)
B = np.log10(data1[3]/(4.*(data1[2])))
R = np.vstack((data1,B))
R = np.transpose(R)
D = R[~np.isnan(R).any(axis=1)]
A = plt.scatter(D[:,3],D[:,2], c=D[:,8])
cbar= plt.colorbar()
cbar.set_label("file", labelpad=+1)
plt.show()
If you could start off by telling us a little bit about the data that you are using that would be great. In order to plot the figure that you want, we must first load the data into some variables. Have you managed to do this?
Check out this example in which the author plots multicolored lines for some guidance.

How to plot independent graphs next to each other using seaborn?

my problem is that I could only find answers for plots sharing the same y-axis units.
My graphs are defined as follows:
#Plot1
sns.set_style("white")
sns.catplot(y="Reaction_cd_positive", x="Flux_cd_positive",
kind="bar",height=4, data=CDP,aspect=1.5)
#Plot2
sns.catplot(y="Reaction_cd_negative",x="Flux_cd_negative",
kind="bar",height=4, data=CDN, aspect=1.5)
Thank you in advance!
Ok, let me translate this. You are using seaborn in a jupyter notebook. You want 2 barplots next to each other within the same figure, instead of two individual figures. Since catplot produces a figure by itself, there are two options.
Create a single catplot with two subplots. To this end you would need to concatenate your two DataFrames into a single one, then use the col argument to split the data into the two subplots.
Create a subplot grid with matplotlib first, then plot a barplot into each of the subplots. This is shown in this question.

Pandas scatter plot

Im new to Python and Pandas but have a CSV file with multiple columns that I have read in to a dataframe. I would like to plot a scatter plot of x=Index and y='data'. Where the index is Index of the dataframe and is a date.
Thanks heaps
Jason
You can use plot_date:
plot_date(df.index, df.data)
Whilst, I guess technically not a scatter plot, you can use the pandas.plot function with point markers drawn on and no lines.
df.plot(marker='o', linewidth=0)
This then allows us to use all of the convenient pandas functionality you desire. e.g. plot two series and different scales, using a single function,
df.plot(marker='o', linewidth=0, secondary_y='y2')
The downside to this is that you lose some of the scatter functionality such as shading and sizing the markers differently.
Still, if your aim is a quick scatter plot, this might be the easiest route.

Categories