|
| 1 | +--- |
| 2 | +Title: '.Carpet()' |
| 3 | +Description: 'Creates carpet plots to visualize interactions between multiple variables in a two-dimensional plot.' |
| 4 | +Subjects: |
| 5 | + - 'Data Science' |
| 6 | + - 'Data Visualization' |
| 7 | +Tags: |
| 8 | + - 'Data' |
| 9 | + - 'Graphs' |
| 10 | + - 'Plotly' |
| 11 | + - 'Python' |
| 12 | + - 'Variables' |
| 13 | +CatalogContent: |
| 14 | + - 'learn-python-3' |
| 15 | + - 'paths/data-science' |
| 16 | +--- |
| 17 | + |
| 18 | +A **carpet plot** is a specialized visualization in Plotly that shows the interaction between two or more independent variables and one or more dependent variables in a two-dimensional plot. Carpet plots distinguish themselves from other plots by enabling more accurate interpolation of data points. They are created using the **`.Carpet()`** method of the `graph_objects` class. |
| 19 | + |
| 20 | +Carpet plots are particularly useful in engineering, scientific research, and data analysis where visualizing relationships between multiple variables is necessary. They can serve as a foundation for other traces like scattercarpet and contourcarpet, which can specify a carpet plot on which they lie. |
| 21 | + |
| 22 | +## Syntax |
| 23 | + |
| 24 | +```pseudo |
| 25 | +plotly.graph_objects.Carpet(a=None, aaxis=None, b=None, baxis=None, carpet=None, cheaterslope=None, color=None, x=None, y=None, ...) |
| 26 | +``` |
| 27 | + |
| 28 | +**Parameters:** |
| 29 | + |
| 30 | +- `a`: An array containing values of the first parameter value |
| 31 | +- `b`: A two-dimensional array of y coordinates at each carpet point |
| 32 | +- `x`: A two-dimensional array of x coordinates at each carpet point. If omitted, the plot is a cheater plot with xaxis hidden by default |
| 33 | +- `y`: A two-dimensional array of y coordinates at each carpet point |
| 34 | +- `aaxis`: Sets the a-axis properties (can be provided as a dictionary) |
| 35 | +- `baxis`: Sets the b-axis properties (can be provided as a dictionary) |
| 36 | +- `carpet`: An identifier for this carpet, so that scattercarpet and contourcarpet traces can specify which carpet plot they lie on |
| 37 | +- `cheaterslope`: The shift applied to each successive row of data in creating a cheater plot (used when x is omitted) |
| 38 | +- `color`: Sets the default color for all elements |
| 39 | + |
| 40 | +**Return value:** |
| 41 | + |
| 42 | +Returns a carpet object that can be added to a figure created with [`plotly.graph_objects.Figure()`](https://www.codecademy.com/resources/docs/plotly/graph-objects/figure). |
| 43 | + |
| 44 | +## Example 1: Carpet Plot with `a` and `b` Parameters |
| 45 | + |
| 46 | +To create a comprehensive carpet plot, the syntax should include `a` and `b` parameter values which define the coordinate system: |
| 47 | + |
| 48 | +```py |
| 49 | +import plotly.graph_objects as go |
| 50 | + |
| 51 | +fig = go.Figure(go.Carpet( |
| 52 | + a = [4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6], |
| 53 | + b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], |
| 54 | + y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10] |
| 55 | +)) |
| 56 | + |
| 57 | +fig.show() |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +In this example, `a` and `b` arrays are included that define the coordinate system for our carpet plot. The values in these arrays correspond to the positions of the data points, creating a grid-like structure. |
| 63 | + |
| 64 | +## Example 2: Styling Carpet Plots for Better Visualization |
| 65 | + |
| 66 | +The carpet plot can be styled using axis properties to make it more visually appealing and easier to interpret: |
| 67 | + |
| 68 | +```py |
| 69 | +import plotly.graph_objects as go |
| 70 | + |
| 71 | +fig = go.Figure(go.Carpet( |
| 72 | + a = [4, 4, 4, 4.5, 4.5, 4.5, 5, 5, 5, 6, 6, 6], |
| 73 | + b = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], |
| 74 | + y = [2, 3.5, 4, 3, 4.5, 5, 5.5, 6.5, 7.5, 8, 8.5, 10], |
| 75 | + aaxis = dict( |
| 76 | + tickprefix = 'a = ', |
| 77 | + ticksuffix = 'm', |
| 78 | + smoothing = 1, |
| 79 | + minorgridcount = 9, |
| 80 | + ), |
| 81 | + baxis = dict( |
| 82 | + tickprefix = 'b = ', |
| 83 | + ticksuffix = 'Pa', |
| 84 | + smoothing = 1, |
| 85 | + minorgridcount = 9, |
| 86 | + ) |
| 87 | +)) |
| 88 | + |
| 89 | +fig.update_layout( |
| 90 | + plot_bgcolor = 'black', |
| 91 | + paper_bgcolor = 'black', |
| 92 | + font_color = 'white' |
| 93 | +) |
| 94 | + |
| 95 | +fig.show() |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +This example enhances the carpet plot by adding custom styling to the `a` and `b` axes. Tick prefixes and suffixes are included to indicate units, the smoothing parameter is adjusted, and minor grid lines are added. Additionally, the background colors are modified for better contrast. |
| 101 | + |
| 102 | +## Example 3: Advanced Carpet Plot with Cheatertype and Custom Layout |
| 103 | + |
| 104 | +For more complex data relationships, different cheatertype values can be used and the layout can be adjusted to better represent the data: |
| 105 | + |
| 106 | +```py |
| 107 | +import plotly.graph_objects as go |
| 108 | + |
| 109 | +fig = go.Figure(go.Carpet( |
| 110 | + a = [4, 4.5, 5, 6], |
| 111 | + b = [1, 2, 3], |
| 112 | + y = [[2, 3, 5.5, 8], |
| 113 | + [3.5, 4.5, 6.5, 8.5], |
| 114 | + [4, 5, 7.5, 10]], |
| 115 | + cheaterslope = -5, |
| 116 | + aaxis = dict(cheatertype = 'index'), |
| 117 | + baxis = dict(cheatertype = 'value') |
| 118 | +)) |
| 119 | + |
| 120 | +fig.show() |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +This example demonstrates using a more advanced configuration where: |
| 126 | + |
| 127 | +1. The `y` parameter is provided as an array of arrays, representing a 2D grid |
| 128 | +2. We set `cheaterslope` to -5, which changes how rows are shifted |
| 129 | +3. The `cheatertype` property is set differently for the a and b axes |
| 130 | + |
| 131 | +The `cheatertype` parameter controls how axes are constructed. When set to `'index'`, the axis uses the index position of values, while 'value' uses the actual values. |
| 132 | + |
| 133 | +> **Note:** The layout of cheater plots depends upon the cheaterslope and axis cheatertype parameters, which can be adjusted to achieve the desired visual representation. |
| 134 | +
|
| 135 | +Carpet plots provide a powerful way to visualize and analyze multi-dimensional data on a two-dimensional surface, especially for applications in engineering, physics, and advanced data analysis. |
0 commit comments