0

I'm trying to plot a contour map using plotly python, but it's not working i getting a blank plot.

Here is my code,

import plotly.graph_objects as go
import pandas as pd
import numpy as np

df = pd.read_csv('./temperature_2d.csv')
x = np.array(df.lon)
y = np.array(df.lat)
z = np.array(df.value).reshape(11, 87)
fig = go.Figure(data =
go.Contour(
    z=z,
    x=x, 
    y=y,
    colorscale = 'Hot',
    contours_coloring='heatmap'
))
fig.show()

Here is the data file that i used to plot temperature_2d.csv

here is the output enter image description here

I'm completely new to things like ploting and all it will be really helpful if someone could explain what's wrong here and how to do this correctly

Abhijith Ea
  • 115
  • 11

1 Answers1

0

Instead of directly plotting i tried to grid your data first.

import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.interpolate import griddata


df = pd.read_csv('./temperature_2d.csv')
x = np.array(df.lon)
y = np.array(df.lat)
z = np.array(df.value)


xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)

X,Y = np.meshgrid(xi,yi)
Z = griddata((x,y),z,(X,Y), method='cubic')



fig = go.Figure(data =
go.Contour(
z=Z,
x=xi,
y=yi,
colorscale = 'Hot',
contours_coloring='heatmap'
))
fig.show()

Example Figure

Akroma
  • 1,150
  • 7
  • 13
  • Thanks that's awesome, can you explain why did my code didn't work.I'm newbie to data visualization so i do not know what exactly my code did there. thank you for your time it was really helpful :) – Abhijith Ea Oct 11 '21 at 12:55
  • I suggest you take a look at these links: https://stackoverflow.com/questions/36013063/what-is-the-purpose-of-meshgrid-in-python-numpy https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html https://plotly.com/python/reference/contour/ – Akroma Oct 11 '21 at 13:37