Carpet Contour Plot using Plotly in Python

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.

Carpet Contour Plot

A carpet contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. X and Y attributes are used to set the x and y coordinates. In case the value of x is missing then it will form a cheater plot. A and B attributes should be used to save the values of the parameter.

Syntax: plotly.graph_objects.Contourcarpet( a=None,b=None,xaxis=None, yaxis=None, z=None)

Parameters:

Name Description
a Sets the x coordinates.
b Sets the y coordinates.
xaxis Sets a reference between this trace’s x coordinates and a 2D cartesian x axis. If “x” (the default value), the x coordinates refer to layout.xaxis. If “x2”, the x coordinates refer to layout.xaxis2
yaxis  Sets a reference between this trace’s y coordinates and a 2D cartesian y axis. If “y” (the default value), the y coordinates refer to layout.yaxis. If “y2”, the y coordinates refer to layout.yaxis2
z Sets the z data.

Example:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.graph_objects as go
  
fig = go.Figure(go.Carpet(
    a=[1, 2, 3, 4, 5, 6],
    b=[6, 5, 4, 3, 2, 1],
    y=[1, 2, 3, 4, 5, 6],
  
    aaxis=dict(
        tickprefix='F = ',
        ticksuffix='N',
        smoothing=0.2,
        minorgridcount=10,
    ),
    baxis=dict(
        tickprefix='P = ',
        ticksuffix='pa',
        smoothing=0.4,
        minorgridcount=9,
    )
))
  
fig.show()

chevron_right


Output:

Adding Contours

In plotly, contours can be added by using the go.Contourcarpet() method of graph_object class. Contours should be added in such a way that the graph body should be in the proper segment.

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import plotly.graph_objects as go
  
fig = go.Figure()
  
fig.add_trace(go.Contourcarpet(
    a = [1, 2, 3, 4, 5, 6],
    b = [6, 5, 4, 3, 2, 1],
    z = [1, 1.96, 5, 6.1028, 4, 5.0625],
    autocontour = False,
    contours = dict(
        start = 1,
        end = 14,
        size = 1
    ),
    line = dict(
        width = 2,
        smoothing = 0
    ),
    colorbar = dict(
       len = 0.4,
        y = 0.25
    )
))
  
  
fig.add_trace(go.Carpet(
    a=[1, 2, 3, 4, 5, 6],
    b=[6, 5, 4, 3, 2, 1],
    y=[1, 2, 3, 4, 5, 6],
  
    aaxis=dict(
        tickprefix='F = ',
        ticksuffix='N',
        smoothing=0.2,
        minorgridcount=10,
    ),
    baxis=dict(
        tickprefix='P = ',
        ticksuffix='pa',
        smoothing=0.4,
        minorgridcount=9,
    )
))
  
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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.