Circular contour map in python - 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

Related

Contour Plotting from the chamber measurement

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

How to draw multiple lines with Seaborn?

I am trying to draw a plot with two lines. Both with different colors. And different labels. This is what I have come up with.
This is code that I have written.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data1 = pd.read_csv("/content/drive/MyDrive/Summer-2020/URMC/training_x_total_data_ones.csv", header=None)
data2 = pd.read_csv("/content/drive/MyDrive/Summer-2020/URMC/training_x_total_data_zeroes.csv", header=None)
sns.lineplot(data=data1, color="red")
sns.lineplot(data=data2)
What am I doing wrong?
Edit
This is how my dataset looks like
So, I just added another color in the second line and that seemed to work.
import random
import numpy as np
import seaborn as sns
mu, sigma = 0, 0.1
s = np.random.normal(mu, sigma, 100)
mu1, sigma1 = 0.5, 1
t = np.random.normal(mu1, sigma1, 100)
sns.lineplot(data= s, color = "red")
sns.lineplot(data= t, color ="blue")
Try specifying the x and y of the call to sns.lineplot?
import pandas as pd
import numpy as np
import seaborn as sns
x = np.arange(10)
df1 = pd.DataFrame({'x':x,
'y':np.sin(x)})
df2 = pd.DataFrame({'x':x,
'y':x**2})
sns.lineplot(data=df1, x='x', y='y', color="red")
sns.lineplot(data=df2, x='x', y='y')
Without doing so, I get a similar plot as yours.

Showing multiple Line Legends in Matplotlib

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)

Python pandas plot linechart with data points

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"])

Seaborn: stripplot x-log scale collapses values

Hi I am trying to use stripplot in seaborn with log scale for the x-axis. It seems that the path I have taken does not work as intended. I would appreciate if someone could help me with that.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
sns.stripplot(x,y)
plt.gca().set_xscale('log')
all the xvalues are collapsed on the right edge of the plot (see plot). I works fine if I set the y-axis to be log.
PS: I would also need to restrict the number of x tick labels.
Thanks.
A scatter plot on a log scale using pyplot.scatter:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
c = np.random.rand(10)
s = 20+np.random.rand(10)*40
plt.scatter(x,y, c=c, s=s, cmap="jet")
plt.gca().set_xscale('log')
plt.xlim(5e-9, 5e-2)
plt.show()
The same scatter plot on a linear scale:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.logspace(-8, -2, 10)
y = np.linspace(0, 100, 10)
c = np.random.rand(10)
s = 20+np.random.rand(10)*40
plt.scatter(x,y, c=c, s=s, cmap="jet")
plt.xlim(-0.003, 0.012)
plt.show()

Categories

Resources