I'm having problems with the following code:
import numpy as np
import pandas as pd
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
xx = np.arange(10)
yy = np.arange(10)
zz = pd.Series(np.arange(10))
fig = plt.figure()
gs = gridspec.GridSpec(1,6)
ax0 = plt.subplot(gs[0,0:2])
zz.plot(ax=ax0)
ax1 = plt.subplot(gs[0,2:4])
zz.plot(ax=ax1)
ax2 = plt.subplot(gs[0,4:])
#plt.plot(xx,yy)
zz.plot(ax=ax2)
plt.savefig('test.png')
plt.close()
I get the error:
IndexError: index 4 is out of bounds for axis 1 with size 4
However, when I substitute the
zz.plot(ax=ax2)
with
plt.plot(xx,yy)
it works fine. Can anyone help me understand why Pandas generates this error?
Related
What I got:
Expected result:
In the chamber measurement, Theta varies from -180:180 and phi varies from 0:180. I have to plot theta from 0:180 and phi from 0:360. How can I plot that in python without altering the dataset file but just in code?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits import mplot3d
import pandas as pd
from matplotlib import rcParams
df= pd.read_csv('Data.csv')
df.head()
Z=df.pivot(index="Phi", columns="Theta", values="E_total").T.values
X_unique = np.sort(df.Theta.unique())
Y_unique = np.sort(df.Phi.unique())
X, Y = np.meshgrid(X_unique, Y_unique)
fig = plt.figure()
ax = fig.add_subplot(111)
cpf = ax.contourf(X,Y,Z,20, cmap=cm.jet)
plt.colorbar(cpf)
ax.set_xlabel('Phi')
ax.set_ylabel('Theta')
enter image description here
I have a 120mm diameter circular disk, where I measure temperature at 20 different locations. These measurement locations are at random places. I am looking for a way to plot it as in attached desired plot link. When I used tricontour, It just plots the random points. I am unable to find a way to fill the circle as in below attached pic. Is there any other way to plot this? Spent lot of time searching for it with no success.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = {"x": [110,50,-85,20,45,0,-80,-30,-105,80], "y":
[0,100,75,-90,20,115,-85,-20,-45,-90],"z":[10,2,6,4,9,12,2,6,4,12]}
x = data['x']
y = data['y']
z = data['z']
f, ax = plt.subplots(1)
plot = ax.tricontourf(x,y,z, 20)
ax.plot(x,y, 'ko ')
circ1 = Circle((0, 0), 120, facecolor='None', edgecolor='r', lw=5)
ax.add_patch(circ1)
f.colorbar(plot)
Example data :
Desired plot:
What I got from tricontour:
There is much data to do a really nice coontour plot, but here is a solution with your data and an example with a substantially larger dataset:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
data = {"x": [110,50,-85,20,45,0,-80,-30,-105,80], "y":
[0,100,75,-90,20,115,-85,-20,-45,-90],"z":[10,2,6,4,9,12,2,6,4,12]}
df = pd.DataFrame(data)
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
ax.set_title("tricontour")
ax.tricontourf(df["x"], df["y"], df["z"],20)
plt.show()
which gives
and for a larger dataframe:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df= pd.DataFrame(np.random.randint(0,1000,size=(1000, 3)), columns=list('XYZ'))
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
ax.set_title("tricontour")
ax.tricontourf(df["X"], df["Y"], df["Z"],20)
plt.show()
which returns
I am trying to display all 4 legends of my line graph, with the Column headers serving as the respective Legend names.
Is there an elegant way of executing this without having to write individual lines of code to plot and label each column?
Examples of my current data set are as follows:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x = pd.Series(np.array([1,2,3,4,5,6,7,8,9,10]))
y = pd.DataFrame(np.random.rand(10,4))
y.columns = ["A","B","C","D"]
fig, ax = plt.subplots(figsize=(10, 7))
ax.plot(x, y, label=True)
Indeed you can use the plot function defined in pandas:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x = pd.Series(np.array([1,2,3,4,5,6,7,8,9,10]))
y = pd.DataFrame(np.random.rand(10,4))
y.columns = ["A","B","C","D"]
y['x'] = x
fig, ax = plt.subplots(figsize=(10, 7))
y.plot(ax=ax)
Can't get the 3D text working to annotate the scatter plot points.
Tried Axes3D.text, plt.text but keep getting 'missing required positional argument 's'. How do you annotate in 3D in a loop?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np
df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#Scatter plot
for i in df.index:
x = df.at[i,'x']
y = df.at[i,'y']
z = df.at[i,'h']
ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
label = df.at[i,'to']
Axes3D.text(x+0.8,y+0.8,z+0.8, label, zdir=x)
TypeError: text() missing 1 required positional argument: 's'
Changing: ax = fig.add_subplot(111, projection='3d')
to: ax = fig.gca(projection='3d')
solved the problem. Used ax.text.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np
df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')
fig = plt.figure()
ax = fig.gca(projection='3d')
#Scatter plot
for i in df.index:
df.set_index('to')
x = df.at[i,'x']
y = df.at[i,'y']
z = df.at[i,'h']
ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
ax.text(x+0.8,y+0.8,z+0.8, df.at[i,'to'], size=10, zorder=1)
I would like to plot a linechart based on column A. Based on Column sig I would like to add some markers to the chart A:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame(np.random.randn(120), columns=list('A'))
data['sig'] = np.NaN
data['sig'] = np.where((data['A'] > 1), data['A'], data['sig'] )
data.plot(grid=True)
plt.show()
I tried to add markevery=data['sig'] to the plot() statement, but it gave me several errors. Any hints?
Why not plot directly in matplotlib?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame(np.random.randn(120), columns=list('A'))
data['sig'] = np.NaN
data['sig'] = np.where((data['A'] > 1), data['A'], data['sig'] )
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(data["A"])
ax.scatter(data.index, data["sig"])