I have a df in the following format:
df = pd.DataFrame({'col1':[11,12,23,21,23,43,23,12,21,34],
'col2':[111,121,213,211,123,143,213,122,210,171],
'col3':[2111,2121,2213,2121,3123,4143,3213,2122,1210,989],
'name':['aa','bb','cc','cc','aa','aa','cc','bb','aa','bb']})
Here is my code to create a simple contour plot using Plotly:
x = np.array(df['col1'])
y = np.array(df['col2'])
z = np.array(df['col3'])
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
grid_x, grid_y = np.meshgrid(xi,yi)
Z = griddata((x,y), z, (grid_x, grid_y), method='linear')
fig = go.Figure(go.Contour(x=xi, y=yi, z=Z, colorscale='Jet'))
fig.show()
This contour plot is a single plot for all of the name
values. I would like to create similar contour plots grouped by name
column. It means, in this example, it should plot 3 contour plots for each 'aa'
, 'bb'
, and 'cc'
values.
Since you want to take only the rows with a certain value in 'name', for each unique value in that column, do this:
for name in np.unique(df['name']):
subdf = df[df['name'] == name]
x = np.array(subdf['col1'])
y = np.array(subdf['col2'])
z = np.array(subdf['col3'])
# ...
Also, as a sidenote, you can include imports to make your code more reproducible.
import pandas as pd
import numpy as np
from scipy.interpolate import griddata
import plotly.graph_objects as go