diff --git a/_collection-base/08-visualization/02-graphs/02-python/02-intro-to-plotly-graphs.md b/_collection-base/08-visualization/02-graphs/02-python/02-intro-to-plotly-graphs.md index a7db93a..8ec5c05 100644 --- a/_collection-base/08-visualization/02-graphs/02-python/02-intro-to-plotly-graphs.md +++ b/_collection-base/08-visualization/02-graphs/02-python/02-intro-to-plotly-graphs.md @@ -11,7 +11,7 @@ type: "tutorial" order: 822.02 level: 3 categories: ["visualization", "graphic-tools", "plotting", "programming", "library-package-module"] -tags: ["python", "plotly", "chart", "development-environment", "IDE", "jupyter", "notebook", "colorscale"] +tags: ["python", "plotly", "chart", "development-environment", "IDE", "jupyter", "notebook", "colorscale", "machine-learning"] attributes: ["installation", "example", "interactive"] --- @@ -102,7 +102,7 @@ Follow to the next step: [Plotly import](#plotly-import) section in this tutoria # Plotly import -Open your python script file or Jupyter notebook and copy paste the folowing code snippet: +Open your python script file or Jupyter notebook and copy paste the following code snippet: ```python # -*- coding: utf-8 -*- @@ -110,15 +110,378 @@ Open your python script file or Jupyter notebook and copy paste the folowing cod import plotly ``` +
+When you perform a simple `import plotly`, it primarily sets up the package namespace and provides access to the top-level attributes and submodules. However, it does not import any of the submodules' contents directly. To use the functionalities provided by specific submodules like `plotly.express` or `plotly.graph_objects`, you still need to import those submodules explicitly. +
+ +# Plotly submodules + +The Plotly library has a modular architecture, which means that its functionality is divided into several submodules. This design allows you to import only the components you need, making your code more efficient and easier to manage. Below is a table listing the core submodules of Plotly, along with a concise description and the import statement for each. + +| submodules | functionality | import statement | +|-------------------------------------------------|-------------------------------|------------------| +| [plotly.express](#plotly-express-wrapper) | High-level interface for quick and easy plotting. | import plotly.express as px | +| [plotly.graph_objects](#plotly-graph-objects) | Detailed and customizable graphing options. | import plotly.graph_objects as go | +| [plotly.subplots](#plotly-subplots) | Tools for creating complex subplot layouts. | from plotly import subplots | +| [plotly.figure_factory](#plotly-figure-factory) | Factory functions for specific types of figures (e.g., dendrograms, clustergrams). | import plotly.figure_factory as ff | +| [plotly.io](#plotly-io) | Input/output operations, including saving figures and configuring renderers. | import plotly.io as pio | +| [plotly.data](#plotly-data) | Sample datasets for testing and learning. | import plotly.data as data | +| [plotly.colors](#plotly-colors) | Utilities for working with colors and color scales. | import plotly.colors as pc | + +
+By importing these submodules as needed, you can efficiently leverage Plotly's powerful data visualization capabilities. +
+ +## **Plotly Express wrapper** + +Plotly Express is a built-in module in the **Plotly** library. It is a high-level interface for creating quick and easy plots. It provides a concise syntax for creating a wide range of interactive visualizations, making it particularly useful for exploratory data analysis. + +**Import Statement:** +```python +import plotly.express as px +``` + + +**Common plot types:** + +| function | description | +|------------------------|--------------------------------| +| `px.scatter()` | creates a scatter plot | +| `px.line()` | creates a line plot | +| `px.bar()` | creates a bar chart | +| `px.histogram()` | creates a histogram | +| `px.pie()` | creates a pie chart | +| `px.sunburst()` | creates a sunburst chart | +| `px.treemap()` | creates a treemap | +| `px.density_heatmap()` | creates a density heatmap | +| `px.density_contour()` | creates a density contour plot | +| `px.scatter_matrix()` | creates a scatter matrix plot | +| `px.imshow()` | creates an image plot (for heatmaps etc.) | + + +### + +
+The `iris` dataset is a classic dataset in the field of machine learning and statistics. It consists of 150 observations of iris flowers, with each observation containing four features: sepal length, sepal width, petal length, and petal width. These features are used to classify the flowers into three species: *Iris-setosa*, *Iris-versicolor* and *Iris-virginica*. The dataset is often used for testing and demonstrating machine learning algorithms and data visualization techniques. +
+ +This example uses the built-in `iris` dataset to create a scatter plot of sepal width vs. sepal length, with different colors representing different species. + +```python +import plotly.express as px + +# Using the built-in iris dataset +df = px.data.iris() + +# Create a scatter plot +fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species') + +# Show the plot +fig.show() +``` + +![python-plotly-express-scatterplot]({{ images_path }}/02-python-plotly-express-scatterplot.png) + + +## **Plotly Graph Objects** + +Plotly Graph Objects is a built-in module in the **Plotly** library. It manages the `Figure` object that represent the entire plotting area. Graph Object provides a detailed and customizable approach to creating plots. It is ideal for creating complex visualizations that require fine-grained control over every aspect of the plot. + +**Import Statement:** +```python +import plotly.graph_objects as go +``` + + +**Common plot types and Figure object:** + +| function | description | +|------------------------|---------------------------------| +| `go.Figure()` | creates a figure object | +| `go.Scatter()` | creates a scatter plot trace | +| `go.Bar()` | creates a bar chart trace | +| `go.Histogram()` | creates a histogram trace | +| `go.Pie()` | creates a pie chart trace | +| `go.Sunburst()` | creates a sunburst chart trace | +| `go.Treemap()` | creates a treemap trace | +| `go.Heatmap()` | creates a heatmap trace | +| `go.Contour()` | creates a contour plot trace | +| `go.Scatter3d()` | creates a 3D scatter plot trace | + + +### + +This example creates a figure and adds a scatter plot trace with both lines and markers. + +```python +import plotly.graph_objects as go + +# Create a figure +fig = go.Figure() + +# Add a scatter plot trace +fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode='lines+markers')) + +# Show the plot +fig.show() +``` + +![python-plotly-go-scatterplot-trace]({{ images_path }}/02-python-plotly-go-scatterplot-trace.png) + + + + +## **Plotly Subplots** + +Plotly Subplots is a built-in module in the **Plotly** library. It provides tools for creating complex subplot layouts, allowing you to create multi-plot figures with ease. This is useful for comparing multiple visualizations side-by-side. + +**Import Statement:** +```python +from plotly import subplots +``` + + +**Subplots function:** + +| function | description | +|----------------------------|-----------------------------------------| +| `subplots.make_subplots()` | creates a figure with multiple subplots | + + +### + +This example creates a figure with two subplots: a scatter plot on the left and a bar chart on the right. + +```python +from plotly import subplots +import plotly.graph_objects as go + +# Create a figure with 1 row and 2 columns of subplots +fig = subplots.make_subplots(rows=1, cols=2) + +# Add a scatter plot trace to the first subplot +fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1) + +# Add a bar chart trace to the second subplot +fig.add_trace(go.Bar(x=[1, 2, 3], y=[6, 5, 4]), row=1, col=2) + +# Show the plot +fig.show() +``` + +![python-plotly-subplots-scatterplot-barchart]({{ images_path }}/02-python-plotly-subplots-scatterplot-barchart.png) + + +## **Plotly Figure Factory** + +Plotly Figure Factory is a built-in module in the **Plotly** library. It contains various factory functions for creating specific types of figures, such as dendrograms, clustergrams and annotated heatmaps. It simplifies the creation of these specialized plots. + +**Import Statement:** +```python +import plotly.figure_factory as ff +``` + + +**Figure Factory functions:** + +| function | description | +|---------------------------------|-------------------------------| +| `ff.create_dendrogram()` | creates a dendrogram | +| `ff.create_clustergram()` | creates a clustergram | +| `ff.create_annotated_heatmap()` | creates an annotated heatmap | +| `ff.create_distplot()` | creates a distribution plot | + + +### + +This example creates an annotated heatmap from a sample data array. + +```python +import plotly.figure_factory as ff + +# Sample data +data = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] + +# Create an annotated heatmap +fig = ff.create_annotated_heatmap(data) + +# Show the plot +fig.show() +``` + +![python-plotly-figure-factory-heatmap]({{ images_path }}/02-python-plotly-figure-factory-heatmap.png) + + +## **Plotly IO** + +Plotly IO is a built-in module in the **Plotly** library. It provides functions for input/output operations, such as saving figures and configuring the default renderer. This is useful for exporting your visualizations and customizing how they are displayed. + +**Import Statement:** +```python +import plotly.io as pio +``` + + +**Input/Output functions:** + +| function | description | +|---------------------|----------------------------------------------| +| `pio.write_image()` | saves a figure as an image file | +| `pio.write_html()` | saves a figure as an HTML file | +| `pio.show()` | displays a figure using a specified renderer | +| `pio.renderers()` | manages and configures default renderers | -## Plotly Express wrapper -Plotly Express is a built-in module in the **Plotly** library and provides a high-level API for smart & efficient creation of interactive graphs. +### + +This example creates a scatter plot using Plotly Express and saves it as a PNG image using `plotly.io`. ```python import plotly.express as px +import plotly.io as pio + +# Create a scatter plot using Plotly Express +fig = px.scatter(x=[1, 2, 3], y=[4, 5, 6]) + +# Save the plot as a PNG image +pio.write_image(fig, 'scatter_plot.png') ``` +![python-plotly-io-scatter-as-png]({{ images_path }}/02-python-plotly-io-scatter-as-png.png) + +

To display a saved plot in Jupyter Notebook after saving it with `plotly.io.write_image` or `fig.write_image`, you can use the `IPython.display` module to display the image file.

+ +
+For a simpler way to save your figures as images, you can use the `write_image` method directly on the figure object. After creating your figure, simply call: +```python +fig.write_image("filename.png") +``` + +Static Image Export in Plotly-Python +
+ +
+Ensure you have `kaleido` installed: +```bash +pip install -U kaleido +``` +or via conda: +```bash +conda install -c conda-forge python-kaleido +``` +to enable rendering the figure to a static image file functionality. This method can be more convenient than using `plotly.io.write_image()`. **Kaleido is now the recommended approach.** +
+ + +## **Plotly Data** + +Plotly Data is a built-in module in the **Plotly** library. It includes sample datasets that can be used for testing and learning. These datasets provide convenient data for trying out Plotly’s functionalities. + +**Import Statement:** +```python +import plotly.data as data +``` + + +**Load Dataset functions:** + +| function | description | +|---------------------|------------------------| +| `data.iris()` | Iris flower dataset | +| `data.tips()` | Tipping data | +| `data.gapminder()` | Gapminder global development data | +| `data.stocks()` | Stock prices data | +| `data.wind()` | Wind patterns data | + + +### + +This example loads the gapminder dataset and creates a line plot showing life expectancy over the years, with different colors representing different continents. + +```python +import plotly.express as px +import plotly.data as data + +# Load the gapminder dataset +df = data.gapminder() + +# Create a line plot +fig = px.line(df, x='year', y='lifeExp', color='continent', + title='Life Expectancy Over the Years by Continent') + +# Show the plot +fig.show() +``` + +![python-plotly-dataset-lineplot]({{ images_path }}/02-python-plotly-dataset-lineplot.png) + +
+In the examples where data is loaded using `plotly.data`, you do not need to `import pandas` separately because Plotly's built-in datasets are returned as pandas `DataFrames`. This allows you to use them directly with Plotly's plotting functions without needing additional data management steps. +
+ +
+However, if you want to perform more complex data manipulations before plotting, you might still need to `import pandas`. + +For the examples given above, you can add additional import for pandas like this: *(assuming you have pandas installed)* +```python +import pandas as pd +``` +Now you can use pandas functions to manipulate the DataFrame `df` if needed.
+*Forexample, you can filter the data to include only records after the year 2000 using pandas, and then creates a line plot showing life expectancy over the years for different continents.* +```python +import plotly.express as px +import plotly.data as data +import pandas as pd + +# Load the gapminder dataset +df = data.gapminder() + +# Manipulate the DataFrame: filter for data after the year 2000 +df_filtered = df[df['year'] > 2000] + +# Create a line plot with the filtered data +fig = px.line(df_filtered, x='year', y='lifeExp', color='continent', + title='Life Expectancy Over the Years by Continent (Post-2000)') + +# Show the plot +fig.show() +``` +
+ + +## **Plotly Colors** + +Plotly Colors is a built-in module in the **Plotly** library. It contains utilities for working with colors and color scales. It helps in customizing the appearance of your plots by providing predefined color scales and functions to manipulate colors. + +**Import Statement:** +```python +import plotly.colors as pc +``` + + +**Load Dataset functions:** + +| function | description | +|--------------------------------|------------------------------------------------| +| `pc.qualitative` | access to qualitative color scales | +| `pc.sequential` | access to sequential color scales | +| `pc.diverging` | access to diverging color scales | +| `pc.find_intermediate_color` | finds an intermediate color between two colors | + + +### + +This example prints the first color from the 'Plotly' qualitative color scale. + +```python +import plotly.colors as pc + +# Print the first color in the 'Plotly' qualitative color scale +print(pc.qualitative.Plotly[0]) +``` + +![python-plotly-colors-get-value]({{ images_path }}/02-python-plotly-colors-get-value.png) + ### *Color Variables* Color variables in **Plotly** have a string type value provided in the one of the following forms: @@ -128,7 +491,7 @@ Color variables in **Plotly** have a string type value provided in the one of th - RGBA color, e.g., `"rgba(r, g, b, a)"`
with 'a' value set up in range from 0.0 (transparent) to 1.0 (solid) - common name, e.g., `"blue"`
see [Built-in Plotly colors](#built-in-plotly-colors) section below -#### Built-in Plotly colors +#### Built-in Plotly named CSS colors *(common names)*
aliceblue, @@ -280,10 +643,46 @@ Color variables in **Plotly** have a string type value provided in the one of th yellow, yellowgreen +Here's an example of a scatter plot using some of the named CSS colors: + +```python +import plotly.express as px +import plotly.graph_objects as go + +# Create a sample scatter plot using discrete named colors +fig = go.Figure() + +# Add scatter plot points with named CSS colors +fig.add_trace(go.Scatter( + x=[1, 2, 3, 4], + y=[10, 11, 12, 13], + mode='markers', + marker=dict(color=['red', 'green', 'blue', 'orange'], size=15) +)) + +# Show the plot +fig.show() +``` + +This example demonstrates how to use discrete named colors in a Plotly scatter plot. +* The `marker` dictionary specifies the `color` and `size` of the markers. + +![python-plotly-named-colors-on-scatterplot]({{ images_path }}/02-python-plotly-named-colors-on-scatterplot.png) #### Built-in Plotly colorscales -Select one of the following named colorscales for coloring the Heatmap section: +To list built-in colorscales in Plotly, try this code snippet: +```python +import plotly.colors as pc + +# Accessing built-in Plotly color scales +color_scales = pc.named_colorscales() + +# Print all available named Plotly color scales +print(", ".join(color_scales)) +``` + +The expected output should be a list of colorscale names: 'aggrnyl', 'agsunset', 'algae', 'amp', 'armyrose', 'balance', 'blackbody', 'bluered', 'blues', 'blugrn', 'bluyl', 'brbg', @@ -306,6 +705,11 @@ Select one of the following named colorscales for coloring the Heatmap section: ![built-in Plotly Colorscales]({{ images_path }}/plotly-colorscales.png) +
+…about the Continuous Color Scales and Color Bars in Python +
+ + #### Custom colorscale You can also create your own colorscale as an array of lists, each composed of two elements. The first refers to the percentile rank, and the second to the applied color. Color can be provided in any [string format](#color-variables) among hex, hsl, rgb or common name. @@ -321,14 +725,27 @@ custom_colorscale = [ ] ``` - - -## Plotly Graph Objects wrapper - -Plotly Graph Objects is a built-in module in the **Plotly** library and manages the `Figure` object that represent the entire plotting area. +Here's an example of using a custom colorscale in a simple Plotly heatmap, where: +* `go.Heatmap()` creates the heatmap using the **data** `z` and the **custom colorscale** `colorscale=custom_colorscale`. ```python import plotly.graph_objects as go + +# Define the custom color scale +custom_colorscale = [[0.00, '#636EFA'], [0.25, '#AB63FA'], [0.50, '#FFFFFF'], [0.75, '#E763FA'], [1.00, '#EF553B']] + +# Sample data for the heatmap +z = [ [1, 20, 30], [20, 1, 60], [30, 60, 1] ] + +# Create the heatmap with the custom color scale +fig = go.Figure(data=go.Heatmap(z=z, colorscale=custom_colorscale)) + +# Show the plot +fig.show() ``` - +![python-plotly-custom-colorscale-heatmap]({{ images_path }}/02-python-plotly-custom-colorscale-heatmap.png) + +

The custom colorscale is displayed as a gradient bar in the legend on the right side of the plot. This gradient bar visually represents the mapping of data values to the specified colors in the custom color scale.

+ +