Related Articles

Related Articles

Contour Plots using Plotly in Python
  • Last Updated : 05 Sep, 2020

A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library.

Contour Plots in Plotly

A contour plot has a function of two variables of curves along which the function has constant values so that these curves join the points with equal values. In contour plot, a 2d contour plot presents contour lines of a 2D numerical array z, i.e. interpolated lines of iso values of z.

Syntax: plotly.graph_objects.Contour(arg=None,colorbar=None, hoverinfo=None, x=None,y=None,**kwargs)

Parameters:

arg: dict of properties compatible with this constructor or an instance of plotly.graph_objects.Contour



colorbar:  plotly.graph_objects.contour.ColorBar instance or dict with compatible properties

x: Sets the x coordinates.

y: Sets the y coordinates.

z: Sets the z coordinates.

hoverinfo: Determines which trace information appear on hover. If none or skip are set, no information is displayed upon hovering. But, if none is set, click and hover events are still fired.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.graph_objects as go
  
feature_x = np.arange(0, 50, 2
feature_y = np.arange(0, 50, 3
  
# Creating 2-D grid of features 
[X, Y] = np.meshgrid(feature_x, feature_y) 
  
Z = np.cos(X / 2) + np.sin(Y / 4
  
fig = go.Figure(data =
     go.Contour(x = feature_x, y = feature_y, z = Z))
  
fig.show()

chevron_right


Output:



Adding Colorscale

In plotly, the colorscale for the contour plot is used to add different colors and can be set using the colorscale parameter.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.graph_objects as go
  
feature_x = np.arange(0, 50, 2
feature_y = np.arange(0, 50, 3
  
# Creating 2-D grid of features 
[X, Y] = np.meshgrid(feature_x, feature_y) 
  
Z = np.cos(X / 2) + np.sin(Y / 4
  
fig = go.Figure(data =
     go.Contour(x = feature_x, y = feature_y, z = Z,
               colorscale='rainbow'
               ))
  
fig.show()

chevron_right


Output:

Adding Labels on Contour Line

Labels can be added using the contours parameter that has a property called showlabels. It determines whether to label the contour lines with their values.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.graph_objects as go
  
feature_x = np.arange(0, 50, 2)
feature_y = np.arange(0, 50, 3)
  
# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)
  
Z = np.cos(X / 2) + np.sin(Y / 4)
  
fig = go.Figure(data=go.Contour(
    x=feature_x, y=feature_y, z=Z,
    contours=dict(
        coloring='lines',
        showlabels=True,)
))
  
fig.show()

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :