LineChart
This example, drawn from the Datawrapper documentation, demonstrates how to create a line chart with a shaded confidence interval around the line.
import pandas as pd
import datawrapper as dw
# Load temperature data from GitHub
url = "https://raw.githubusercontent.com/chekos/datawrapper/main/tests/samples/line/land-temps.csv"
df = pd.read_csv(url)
chart = dw.LineChart(
# Chart title
title="Global land temperature in July, 1753-2015",
# Data source attribution
source_name="Berkeley Earth",
source_url="http://berkeleyearth.org/data/",
# Data from pandas DataFrame
data=df,
# Set the suffix on our values
transformations=dw.Transform(
column_format=[
dw.ColumnFormat(
column="LandAverageTemperature",
number_append=" °C",
)
]
),
# Set the range
custom_range_y=[8, 21],
# Format Y-axis grid labels with no decimal places
y_grid_format="0",
# And now the tooltip with a bit more...
tooltip_number_format="00.00",
tooltip_x_format="YYYY",
# Configure the main temperature line's color
color_category={
"LandAverageTemperature": "#1d81a2",
},
lines=[
# Style the main line
dw.Line(
column="LandAverageTemperature",
width=dw.LineWidth.THIN,
interpolation=dw.LineInterpolation.CURVED,
),
# Hide the other two
dw.Line(
column="lower",
width=dw.LineWidth.INVISIBLE,
),
dw.Line(
column="upper",
width=dw.LineWidth.INVISIBLE,
),
],
# Add shaded confidence interval area
area_fills=[
dw.AreaFill(
from_column="lower",
to_column="upper",
color="#cccccc",
opacity=0.45,
)
],
)
chart.create()
Reference
Parameter |
Default |
Description |
Type |
|---|---|---|---|
|
PydanticUndefined |
Custom area fills |
list[UnionType[AreaFill, dict[str, Any]]] |
|
“” |
The alternative text for screen readers |
str |
|
False |
Whether the chart should automatically flip to dark mode when the user’s system is in dark mode |
bool |
|
0 |
The base color for lines (palette index or hex string) |
str or int |
|
“” |
The byline that appears below the chart |
str |
|
None |
The chart ID after creation (populated by create() method) |
str or None |
|
“d3-lines” |
The type of datawrapper chart to create |
Literal[d3-lines] |
|
PydanticUndefined |
A mapping of layer names to colors |
dict[str, str] |
|
False |
Whether or not to draw a connector line between lines and labels |
bool |
|
PydanticUndefined |
A dictionary of custom tags to attach to the chart |
dict[str, Any] |
|
None |
Custom range for X-axis as [min, max]. Overrides automatic range calculation. |
list[Any] or tuple[Any, Any] or None |
|
None |
Custom range for Y-axis as [min, max]. Overrides automatic range calculation. |
list[Any] or tuple[Any, Any] or None |
|
None |
Custom tick mark positions for X-axis. List of values where ticks should appear. |
list[Any] or None |
|
None |
Custom tick mark positions for Y-axis. List of values where ticks should appear. |
list[Any] or None |
|
True |
Whether to invert colors in dark mode |
bool |
|
PydanticUndefined |
The data to use for the chart |
DataFrame or list[dict] |
|
False |
Whether to allow PNG download |
bool |
|
False |
Whether to allow PDF download |
bool |
|
False |
Whether to allow SVG download |
bool |
|
False |
Whether to allow embedding |
bool |
|
False |
Whether to attribute the chart to datawrapper |
bool |
|
True |
Whether to allow other users to fork this visualization |
bool |
|
False |
Whether to allow data downloads |
bool |
|
False |
Whether or not to hide the title |
bool |
|
“linear” |
The interpolation method to use when drawing lines |
LineInterpolation (LINEAR, STEP, STEP_AFTER, …) or str |
|
“” |
The intro text that appears above the chart |
str |
|
False |
Whether or not to color line category labels the same as the line |
bool |
|
0 |
The amount of margin to leave for the right hand side for labels. Zero is automatically calculated. |
int |
|
“en-US” |
The locale of the chart, which defines decimal and thousand separators as well as translations of month and weekday names. |
str |
|
PydanticUndefined |
Custom configuration for individual lines |
list[UnionType[Line, dict[str, Any]]] |
|
False |
Whether to show a logo |
bool |
|
“” |
The id of the logo to show |
str |
|
“” |
The footnotes that appear below the chart |
str |
|
300 |
The fixed height of the plot |
int or float |
|
“fixed” |
How to set the plot height (managed by PlotHeight serializer) |
PlotHeightMode (FIXED, RATIO) or str |
|
0.5 |
The ratio of the plot height |
float |
|
PydanticUndefined |
A list of range annotations to display on the chart |
Sequence[UnionType[RangeAnnotation, dict[Any, Any]]] |
|
“linear” |
How to scale the y-axis |
Literal[linear, log] |
|
False |
Whether to show social media share buttons |
bool |
|
“” |
What URL to share |
str |
|
True |
Whether or not to show tooltips on hover |
bool |
|
“” |
The source name that appears below the chart |
str |
|
“” |
The source URL that appears below the chart |
str |
|
False |
Whether or not to stack the color legend |
bool |
|
PydanticUndefined |
A list of text annotations to display on the chart |
Sequence[UnionType[TextAnnotation, dict[Any, Any]]] |
|
“datawrapper” |
The theme of the chart |
str |
|
“” |
The headline that appears above the chart |
str |
|
“” |
The format of the number tooltip. Use DateFormat for temporal data, NumberFormat for numeric data, or provide custom format strings. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str |
|
“” |
The format for the x-axis tooltips. Use DateFormat for temporal data, NumberFormat for numeric data, or provide custom format strings. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str |
|
PydanticUndefined |
Transform or dict[str, Any] |
|
|
False |
Whether to color number values labels the same as the line |
bool |
|
“” |
The number format for value labels. Use DateFormat for temporal data, NumberFormat for numeric data, or provide custom format strings. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str |
|
None |
The column to use for the x-axis |
str or None |
|
“off” |
X-axis grid display setting. Controls vertical grid lines. |
GridDisplay (OFF, ON, TICKS, …) or str or bool or None |
|
None |
Format string for X-axis grid labels. Supports date and number formats. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str or None |
|
“on” |
Y-axis grid display setting. Controls horizontal grid lines. |
GridDisplay (OFF, ON, TICKS, …) or str or bool or None |
|
None |
Format string for Y-axis grid labels. Supports number formats. |
NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str or None |
|
“left” |
Which side to put the y-axis labels on |
GridLabelAlign (LEFT, RIGHT) or str |
|
“auto” |
The labeling of the y grid labels |
GridLabelPosition (AUTO, INSIDE, OUTSIDE, …) or str |
|
True |
Whether or not to subdivide a log scale |
bool |