Area Plots in Python Examples
There are various examples of Area Plots in Python . here we are discussing some generally used examples of Area Plots in Python those are following.
- Interactive Area Plot of Iris Sepals
- Visualize Random Data with Plotly Express
- Area Plot with Plotly Express
- Customizing Area Charts
- Stacked Area Chart
- Plotly Express Filled Area Chart with Range
Interactive Area Plot of Iris Sepals
In this example code uses the Plotly Express library to create an area plot from the Iris dataset, with sepal width on the x-axis, sepal length on the y-axis, and different species differentiated by color. The hover information includes petal width. The resulting plot is displayed using fig.show()
.
Python3
import plotly.express as px df = px.data.iris() fig = px.area(df, x = "sepal_width" , y = "sepal_length" , color = "species" , hover_data = [ 'petal_width' ],) fig.show() |
Output:
Visualize Random Data with Plotly Express
In this example The provided Python code utilizes the Plotly Express library to generate an area plot. It first imports the necessary modules, Plotly Express as px
and NumPy as np
. The code then sets a random seed for reproducibility, generates two arrays of 100 random integers each using NumPy’s random.randint
function.
Python3
import plotly.express as px import numpy as np # creating random data through randomint # function of numpy.random np.random.seed( 42 ) random_x = np.random.randint( 1 , 101 , 100 ) random_y = np.random.randint( 1 , 101 , 100 ) fig = px.area(x = random_x, y = random_y) fig.show() |
Output:
Area Plot with Plotly Express
In this example Python code uses the Plotly Express library to create an area plot. It loads a sample dataset of restaurant tips using px.data.tips()
, and then generates an area plot with time on the x-axis, day on the y-axis, and colors representing the total bill. The resulting plot is displayed using fig.show()
Python3
import plotly.express as px df = px.data.tips() fig = px.area(df, x = 'time' , y = 'day' , color = "total_bill" ) fig.show() |
Output:
Customizing Area Charts
In this example code uses Plotly Express to create a customized filled area chart with two lines. It defines x and y data, creates a line chart with a filled area for the first line, adds a second line with a filled area between the two lines, and updates the layout with a title and axis labels. The resulting plot is displayed.
Python3
import plotly.express as px # Sample data x_data = [ 1 , 2 , 3 , 4 , 5 ] y1_data = [ 10 , 15 , 13 , 17 , 10 ] y2_data = [ 8 , 13 , 11 , 15 , 8 ] # Create a basic line chart fig = px.line(x = x_data, y = y1_data, line_shape = 'linear' , labels = { 'y' : 'Y-axis' }) # Update the trace to fill the area between the line and the axis fig.update_traces(fill = 'tozeroy' , line_color = 'rgba(0,100,80,0.2)' ) # Add another line trace fig.add_trace(px.line(x = x_data, y = y2_data, line_shape = 'linear' ).update_traces(fill = 'tonexty' , line_color = 'rgba(0,100,80,0.2)' ).data[ 0 ]) # Update layout fig.update_layout(title = 'Customized Filled Area Chart' , xaxis_title = 'X-axis' ) # Show the plot fig.show() |
Output :
Stacked Area Chart
In this example This code uses the Plotly library to create a stacked area chart. It defines sample data with x and two y series, creates the chart using Plotly Express, and customizes labels and titles. Finally, it displays the chart with x and y-axis labels using `fig.show()`.
Python3
import plotly.express as px # Sample data x_data = [ 1 , 2 , 3 , 4 , 5 ] y1_data = [ 10 , 15 , 13 , 17 , 10 ] y2_data = [ 8 , 13 , 11 , 15 , 8 ] # Create a stacked area chart fig = px.area(x = x_data, y = [y1_data, y2_data], stackgroup = 'one' , labels = { 'y' : 'Y-axis' }) # Update layout fig.update_layout(title = 'Stacked Area Chart' , xaxis_title = 'X-axis' ) # Show the plot fig.show() |
Output :
Plotly Express Filled Area Chart with Range
In this example code uses Plotly Express to create a filled area chart with a range. It defines sample data for x-axis and three sets of y-values representing mean, lower bound, and upper bound. The code then generates a scatter plot for the mean values and overlays two additional scatter plots for the lower and upper bounds. Finally, it updates the layout and traces to create a filled area between the upper and lower bounds with specified colors.
Python3
import plotly.express as px # Sample data x_data = [ 1 , 2 , 3 , 4 , 5 ] y_mean = [ 10 , 15 , 13 , 17 , 10 ] y_lower = [ 8 , 12 , 10 , 14 , 8 ] y_upper = [ 12 , 18 , 15 , 20 , 12 ] # Create a filled area chart with a range fig = px.scatter(x = x_data, y = y_mean, labels = { 'y' : 'Y-axis' }) fig.add_trace(px.scatter(x = x_data, y = y_lower).data[ 0 ]) fig.add_trace(px.scatter(x = x_data, y = y_upper).data[ 0 ]) # Update layout fig.update_layout(title = 'Filled Area Chart with Range' , xaxis_title = 'X-axis' ) # Fill the area between the upper and lower bounds fig.update_traces(fill = 'toself' , fillcolor = 'rgba(0,100,80,0.2)' , line_color = 'rgba(255,255,255,0)' ) # Show the plot fig.show() |
Output
Filled area chart using plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization library.
Contact Us