Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to shade a chart above a specific Y value in Python Plotly?
Plotly is a powerful plotting library in Python that enables you to create interactive visualizations. Sometimes you need to highlight specific regions of your chart by shading areas above or below certain Y values to emphasize important thresholds or ranges.
In this tutorial, we will show how to shade a chart above a specific Y value using Plotly's add_hrect() method.
Understanding the Methods
To shade chart areas, we use these key Plotly methods:
add_hrect() ? Adds a horizontal rectangle to shade regions based on Y-axis values
add_vline() ? Adds vertical reference lines
plotly.express ? Creates the base scatter plot
Basic Example
Let's create a scatter plot and shade the area above a specific Y value ?
import plotly.express as px
import pandas as pd
# Create sample data
data = {
'model': ['moto', 'sony', 'samsung', 'redmi'],
'price': [20000, 25000, 30000, 28000],
'year': [2018, 2017, 2015, 2019]
}
df = pd.DataFrame(data)
# Create scatter plot
fig = px.scatter(df, x='price', y='year', title='Phone Models by Price and Year')
# Add horizontal shading above year 2017
fig.add_hrect(
y0=2017, # Start Y value
y1=2020, # End Y value (above the data range)
fillcolor="lightblue",
opacity=0.3,
line_width=0
)
# Optional: Add a reference line at the threshold
fig.add_hline(
y=2017,
line_dash="dash",
line_color="blue",
annotation_text="Threshold: 2017"
)
fig.show()
Advanced Shading with Multiple Regions
You can add multiple shaded regions with different colors and opacities ?
import plotly.express as px
import pandas as pd
# Create more sample data
data = {
'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
'sales': [100, 150, 120, 180, 200, 160],
'target': [130, 130, 130, 130, 130, 130]
}
df = pd.DataFrame(data)
# Create line plot
fig = px.line(df, x='month', y='sales', title='Monthly Sales vs Target')
fig.add_scatter(x=df['month'], y=df['target'], mode='lines', name='Target', line_dash='dash')
# Shade area above target (good performance)
fig.add_hrect(
y0=130, # Target line
y1=250, # Upper limit
fillcolor="green",
opacity=0.2,
line_width=0,
annotation_text="Above Target",
annotation_position="top right"
)
# Shade area below target (poor performance)
fig.add_hrect(
y0=0, # Lower limit
y1=130, # Target line
fillcolor="red",
opacity=0.2,
line_width=0,
annotation_text="Below Target",
annotation_position="bottom left"
)
fig.show()
Key Parameters
The add_hrect() method accepts these important parameters:
| Parameter | Description | Example |
|---|---|---|
y0 |
Starting Y coordinate | y0=2017 |
y1 |
Ending Y coordinate | y1=2020 |
fillcolor |
Fill color of rectangle | fillcolor="lightblue" |
opacity |
Transparency level (0-1) | opacity=0.3 |
line_width |
Border width (0 for no border) | line_width=0 |
Conclusion
Use add_hrect() to shade chart regions above specific Y values in Plotly. This technique helps highlight important data ranges and thresholds in your visualizations. Combine with reference lines for better clarity.
