Contour Plotting from the chamber measurement - python

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

Related

Circular contour map in python

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

White line in contour plot in cartopy on center_longitude

I'm plotting some filled contours with Cartopy and Matplotlib. The data is on a latitude/longitude grid, and when plotting on a cartopy projection, a white line runs down the middle of the figure, or wherever I set "central_longitude" into in ccrs.PlateCarree()
Here is a quick setup that shows what I'm talking about. Using the code:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
lon = np.arange(0, 360, 1)
lat = np.arange(-90, 90, 1)
data = np.zeros((180, 360))
fig = plt.figure()
ax = plt.subplot(projection=ccrs.PlateCarree())
ax.contourf(lon, lat, data)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
plt.show()
Which produces the image:
Is there a way to remove this white line?
You should use cartopy.util.add_cyclic_point so that contourf sees the data as continuous in the x-direction and the white line will disappear:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.util import add_cyclic_point
lon = np.arange(0, 360, 1)
lat = np.arange(-90, 90, 1)
data = np.zeros((180, 360))
data, lon = add_cyclic_point(data, coord=lon)
fig = plt.figure()
ax = plt.subplot(projection=ccrs.PlateCarree())
ax.contourf(lon, lat, data)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
plt.show()

Axes3D.text() Annotate 3D Scatter Plot

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)

White pcolor introduces white bar

I have the following script
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.show()
Which results into this image
How can I remove the white bar at the very top?
You have to manually set the x and y limits sometimes when you're using pcolor.
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.ylim(0, 24)
plt.show()
I am assuming here that your matrix is not a jagged matrix:
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
nrow, ncol = data.shape
heatmap = plt.pcolor(data)
# put the major ticks
heatmap.axes.set_xticks(np.arange(ncol), minor=False)
heatmap.axes.set_yticks(np.arange(nrow), minor=False)
heatmap.axes.set_xlim(0,ncol) # Assuming a non jagged matrix
heatmap.axes.set_ylim(0,nrow)
plt.show()
Just simple change. np.random.rand(24,7) replace to np.random.rand(25,7)
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(25,7)
heatmap = plt.pcolor(data)
plt.show()
Output:
Or add axis Like plt.axis([0,7,0,24])
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(24,7)
heatmap = plt.pcolor(data)
plt.axis([0,7,0,24])
plt.show()
Output:

Pandas Series Plot and GridSpec

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?

Categories

Resources