Problem when adding a third line to subplot - matplotlib

I need to create subplots that represents 3 variables (line plots) in each one, but the problem is that when i add the third one, the subplot goes wrong (the line is displayed wrong). This doesnt happen when i include only x and two variables.
In this example, the first subplot is ok with the variables x , y1 y2, but when i add y3 it goes wrong. x, y1, y2, and y3 are columns of a data frame where x is 01, 02, 03 and 04 and the others are values for those months. Thanks
# Subplot 1
#axis[0, 0].plot(x, y1, y2, y3) #WORKS WRONG WHEN ADDING Y3

You need to repeat the x in the call to plot:
ax.plot(x, y1, x, y2, x, y3)
or repeatedly call plot on the same axes object:
for y in (y1, y2, y3):
ax.plot(x, y)
See Plotting multiple sets of data in the docs for plot.

Related

Create 48 plots by iterating through columns of pandas dataframe - How to increase column index in a loop?

I have a Pandas Dataframe with 96 columns x 100 rows which looks like this:
x1, y1, x2, y2, x3, y3, ..., x48, y48
0.5, 521, 1.8, 625, 5.5, 856, ..., 2.5, 453
0.6, 556, 1.9, 695, 5.6, 1023, ..., 2.6, 569
I want to plot y1 against x1, then y2 against x2, y3 against x3, and so on.
I'm really struggling with increasing the column indices. This code doesn't work:
df = pd.read_csv(r'xxx.csv', delimiter=';')
for col in df:
x=col
y=col+1
df.plot(x, y)
plt.show()
TypeError: can only concatenate str (not "int") to str
I get the problem, but don't know how to solve it :(
Here is an approach (highly inspired by Trenton McKinney).
We start by selecting/zipping the x columns and y columns with pandas.DataFrame.filter and pandas.DataFrame.columns, then we use matplotlib.axes.Axes.scatter (feel free to put any other kind of plots).
Try this :
import pandas as pd
import matplotlib.pyplot as plt
# df = pd.read_csv(r'xxx.csv', delimiter=';')
combos = list(zip(df.filter(like="x").columns, df.filter(like="y").columns))
fig, axes = (
plt.subplots(nrows=int(len(df.filter(like="x").columns)/2),
ncols=int(len(df.filter(like="y").columns)/2),
figsize=(8, 4))
)
axes = axes.flat
for (x, y), ax in zip(combos, axes):
ax.scatter(df[x], df[y])
ax.set(title=f'{x} vs. {y}', xlabel=x, ylabel=y)
plt.tight_layout()
plt.show()
# Output :
# Used Input:
print(df)
x1 y1 x2 y2 x3 y3 x48 y48
0 0.5 521 1.8 625 5.5 856 2.5 453
1 0.6 556 1.9 695 5.6 1023 2.6 569

Creating a 3 category binned colormap for continuous variable for matplotlib plot?

I have two measurements x & y and one calculation f(x, y) where f(x, y) can be broken into 3 categories: Acceptable(>1.2), At Risk (1 < x <=1.2), and Not Acceptable (<=1). I was wondering what is the best way to bin and plot this where f(x,y) is the colormap for a y v x scatter plot.
Thanks!
I'm not sure if this is exactly what you are after but sounds like you might be interested in pd.cut

matplotlib multiple plots in one figure strange happen

I tried to plot a distribution pdf and cdf in one plot. If plot together, pdf and cdf are not matched. If plot separately, they will match. Why? You can see both green curves from same equation, but shows different shape...
def MBdist(n,loct,scale):
data = maxwell.rvs(loc=loct, scale=scale, size=n)
params = maxwell.fit(data, floc=0)
return data, params
if __name__ == '__main__':
data,para=MBdist(10000,0,0.5)
plt.subplot(211)
plt.hist(data, bins=20, normed=True)
x = np.linspace(0, 5, 20)
print x
plt.plot(x, maxwell.pdf(x, *para),'r',maxwell.cdf(x, *para), 'g')
plt.subplot(212)
plt.plot(x, maxwell.cdf(x, *para), 'g')
plt.show()
You also don't pass in an 'x' to go with the second line so it is plotting against index. It should be
plt.plot(x, maxwell.pdf(x, *para),'r',x, maxwell.cdf(x, *para), 'g')
This interface is a particularly magical bit of arg-parsing that was mimicked from MATLAB. I would suggest
fig, ax = plt.subplots()
ax.plot(x, maxwell.pdf(x, *para),'r')
ax.plot(x, maxwell.cdf(x, *para), 'g')
which while a bit more verbose line-wise is much clearer.

Scatter plot (2D), which shows a dotted circle and other 2D-shapes made by geometrical functions with ipython, numpy and matplotlib

I would to create an array with the "shape" (n, 2), which is creating a dotted circle, when plotted on a scatterplot.
This would be the wanted form of the array:
array([ (x1, y1),
(x2, y2),
(x3, y3),
(x4, y4),
....
(xN, yN),
])
This is how it more or less should look like:
I am as well looking for ways to extrapolate this to the 3rd (sphere) and Nth dimension displaying the data set with SPLOM (scatter plot matrix) or paralell coordinates. Generally I am looking for ways to draw automatically (thus, not by hand, but maybe close to) shapes (like circles or other functions) and clusters in N-dimensions. Which would like this:
array([ (x1, y1, z1, a1, .... n1),
(x2, y2, z2, a2, .... n2),
(x3, y3, z3, a3, .... n3),
(x4, y4, z4, a4, .... n4),
....
(xN, yN, zN, aN, .... nN),
])
Here is a method for the circle in 2D.
def CreateCircleArray(radius=1):
theta = np.linspace(0, 2*np.pi, 50)
x = radius * np.cos(theta)
y = radius * np.sin(theta)
return np.array([x, y]).T
def PlotArray(array):
ax = plt.subplot(111, aspect="equal")
ax.scatter(array[:, 0], array[:, 1], color="k")
plt.show()
PlotArray(CreateCircleArray())
Going to 3D is a case of extending the function to include the azimuthal angle allowing you to return x, y, z and hence scatter plot in 3D.
Past that you will need to write a function that generates the Cartesian coordinates of the n-sphere. There is an SO post that is related but I am unsure how useful it will be.
For other shapes you will need to think about what coordinate system is appropriate, for instance defining a square in this way is difficult if its edges are parallel to the axis.
Good luck

How multiple plots can be compared

I have two plots. and i have to compare these plots by making one plot.
example
plot1 x range (0 - 50), some y-range
plot2 x range (50 - 100), some y-range
resultant plot x range (0 -100)
How this can be done?
Am I missing something or can you just do:
plot(x1,y1);
points(x2-50,y2);
More generally, you can plot any 2 things together. You have to make the x and y scales constant across both plots. Something like this should work:
# assume I have vars: x1 and x2 and y1 and y2
# figure out the range of all y vars
yRange = range(y1,y2)
xRange = range(x1,x2)
plot(xRange,yRange, type='n') #creates the axes but doesn't put any points on them
points(x1,y1)
points(x2,y2, col='red')
My apologies, I thought this was in the R group and answered based on that.