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

area_fills

PydanticUndefined

Custom area fills

list[UnionType[AreaFill, dict[str, Any]]]

aria_description

“”

The alternative text for screen readers

str

auto_dark_mode

False

Whether the chart should automatically flip to dark mode when the user’s system is in dark mode

bool

base_color

0

The base color for lines (palette index or hex string)

str or int

byline

“”

The byline that appears below the chart

str

chart_id

None

The chart ID after creation (populated by create() method)

str or None

chart_type

“d3-lines”

The type of datawrapper chart to create

Literal[d3-lines]

color_category

PydanticUndefined

A mapping of layer names to colors

dict[str, str]

connector_lines

False

Whether or not to draw a connector line between lines and labels

bool

custom

PydanticUndefined

A dictionary of custom tags to attach to the chart

dict[str, Any]

custom_range_x

None

Custom range for X-axis as [min, max]. Overrides automatic range calculation.

list[Any] or tuple[Any, Any] or None

custom_range_y

None

Custom range for Y-axis as [min, max]. Overrides automatic range calculation.

list[Any] or tuple[Any, Any] or None

custom_ticks_x

None

Custom tick mark positions for X-axis. List of values where ticks should appear.

list[Any] or None

custom_ticks_y

None

Custom tick mark positions for Y-axis. List of values where ticks should appear.

list[Any] or None

dark_mode_invert

True

Whether to invert colors in dark mode

bool

data

PydanticUndefined

The data to use for the chart

DataFrame or list[dict]

download_image

False

Whether to allow PNG download

bool

download_pdf

False

Whether to allow PDF download

bool

download_svg

False

Whether to allow SVG download

bool

embed

False

Whether to allow embedding

bool

force_attribution

False

Whether to attribute the chart to datawrapper

bool

forkable

True

Whether to allow other users to fork this visualization

bool

get_the_data

False

Whether to allow data downloads

bool

hide_title

False

Whether or not to hide the title

bool

interpolation

“linear”

The interpolation method to use when drawing lines

LineInterpolation (LINEAR, STEP, STEP_AFTER, …) or str

intro

“”

The intro text that appears above the chart

str

label_colors

False

Whether or not to color line category labels the same as the line

bool

label_margin

0

The amount of margin to leave for the right hand side for labels. Zero is automatically calculated.

int

language

“en-US”

The locale of the chart, which defines decimal and thousand separators as well as translations of month and weekday names.

str

lines

PydanticUndefined

Custom configuration for individual lines

list[UnionType[Line, dict[str, Any]]]

logo

False

Whether to show a logo

bool

logo_id

“”

The id of the logo to show

str

notes

“”

The footnotes that appear below the chart

str

plot_height_fixed

300

The fixed height of the plot

int or float

plot_height_mode

“fixed”

How to set the plot height (managed by PlotHeight serializer)

PlotHeightMode (FIXED, RATIO) or str

plot_height_ratio

0.5

The ratio of the plot height

float

range_annotations

PydanticUndefined

A list of range annotations to display on the chart

Sequence[UnionType[RangeAnnotation, dict[Any, Any]]]

scale_y

“linear”

How to scale the y-axis

Literal[linear, log]

share_buttons

False

Whether to show social media share buttons

bool

share_url

“”

What URL to share

str

show_tooltips

True

Whether or not to show tooltips on hover

bool

source_name

“”

The source name that appears below the chart

str

source_url

“”

The source URL that appears below the chart

str

stack_color_legend

False

Whether or not to stack the color legend

bool

text_annotations

PydanticUndefined

A list of text annotations to display on the chart

Sequence[UnionType[TextAnnotation, dict[Any, Any]]]

theme

“datawrapper”

The theme of the chart

str

title

“”

The headline that appears above the chart

str

tooltip_number_format

“”

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

tooltip_x_format

“”

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

transformations

PydanticUndefined

Transform or dict[str, Any]

value_label_colors

False

Whether to color number values labels the same as the line

bool

value_labels_format

“”

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

x_column

None

The column to use for the x-axis

str or None

x_grid

“off”

X-axis grid display setting. Controls vertical grid lines.

GridDisplay (OFF, ON, TICKS, …) or str or bool or None

x_grid_format

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

y_grid

“on”

Y-axis grid display setting. Controls horizontal grid lines.

GridDisplay (OFF, ON, TICKS, …) or str or bool or None

y_grid_format

None

Format string for Y-axis grid labels. Supports number formats.

NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str or None

y_grid_label_align

“left”

Which side to put the y-axis labels on

GridLabelAlign (LEFT, RIGHT) or str

y_grid_labels

“auto”

The labeling of the y grid labels

GridLabelPosition (AUTO, INSIDE, OUTSIDE, …) or str

y_grid_subdivide

True

Whether or not to subdivide a log scale

bool