hvPlot.line#
- hvPlot.line(x=None, y=None, **kwds)[source]#
The line plot connects the points with a continuous curve.
Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.line.html
Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html
- Parameters:
- xstring, optional
Field name(s) to draw x-positions from. If not specified, the index is used. Can refer to continuous and categorical data.
- ystring or list, optional
Field name(s) to draw y-positions from. If not specified, all numerical fields are used.
- bystring, optional
A single column or list of columns to group by. All the subgroups are visualized.
- groupby: string, list, optional
A single field or list of fields to group and filter by. Adds one or more widgets to select the subgroup(s) to visualize.
- colorstr or array-like, optional.
The color for each of the series. Possible values are:
A single color string referred to by name, RGB or RGBA code, for instance ‘red’ or ‘#a98d19.
A sequence of color strings referred to by name, RGB or RGBA code, which will be used for each series recursively. For instance [‘green’,’yellow’] each field’s line will be filled in green or yellow, alternatively. If there is only a single series to be plotted, then only the first color from the color list will be used.
- **kwdsoptional
Additional keywords arguments are documented in Plotting Options. Run
hvplot.help('line')
for the full method documentation.
- Returns:
holoviews.element.Curve
/ Panel objectYou can print the object to study its composition and run:
import holoviews as hv hv.help(the_holoviews_object)
to learn more about its parameters and options.
References
Bokeh: https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/line.html
HoloViews: https://holoviews.org/reference/elements/bokeh/Curve.html
Pandas: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.line.html
Matplotlib: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
Seaborn: https://seaborn.pydata.org/generated/seaborn.lineplot.html
Backend-specific styling options#
alpha, color, hover_alpha, hover_color, hover_line_alpha, hover_line_cap, hover_line_color, hover_line_dash, hover_line_dash_offset, hover_line_join, hover_line_width, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, muted, muted_alpha, muted_color, muted_line_alpha, muted_line_cap, muted_line_color, muted_line_dash, muted_line_dash_offset, muted_line_join, muted_line_width, nonselection_alpha, nonselection_color, nonselection_line_alpha, nonselection_line_cap, nonselection_line_color, nonselection_line_dash, nonselection_line_dash_offset, nonselection_line_join, nonselection_line_width, selection_alpha, selection_color, selection_line_alpha, selection_line_cap, selection_line_color, selection_line_dash, selection_line_dash_offset, selection_line_join, selection_line_width, visible
alpha, c, color, linestyle, linewidth, lw, marker, ms, visible
Examples#
Basic line plot#
This example shows how to create a simple line plot.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
df.hvplot.line(x="x", y="y")
Multiple line plot#
This dataset consists of a date
column and 6 different stocks columns. In this example we only set x
, hvPlot infers it should attempt to display all the remaining columns as lines. Alternatively, we could explicitly set y
to the list of columns.
import hvplot.pandas # noqa
df = hvplot.sampledata.stocks("pandas")
df.hvplot.line(x="date", group_label="Stocks")
Grouping by categories#
To distinguish categories visually, you can use the by
option. This automatically colors lines based on the specified column, returning a HoloViews NdOverlay.
Note
The color
cannot be used to vectorize coloring each individual line.
import hvplot.pandas # noqa
df = hvplot.sampledata.stocks("pandas").set_index("date").melt(ignore_index=False, var_name="stock")
print(df.head(2))
df.hvplot.line(by="stock")
stock value
date
2019-01-01 Apple 1.00000
2019-01-08 Apple 1.01399
Line plot with markers#
The Bokeh plotting backend doesn’t offer options to style the markers of a line glyph. So we simply overlay a scatter
plot. Visit the scatter
reference to find how to customize the marker style.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
df.hvplot.line(x="x", y="y") *\
df.hvplot.scatter(x="x", y="y", marker="*", s=100, padding=0.1)
The Matplotlib plotting backend offers some simple ways to directly style line markers.
import hvplot.pandas # noqa
import pandas as pd
hvplot.extension("matplotlib")
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
df.hvplot.line(x="x", y="y", marker="*", ms=10, padding=0.1)
Line dash#
The Bokeh plotting backend accepts these values for the line_dash
keyword:
One of
'solid'
,'dashed'
,'dotted'
,'dotdash'
,'dashdot'
.A dash pattern (following the HTML5 Canvas dash specification) as a tuple of integers, which specify the lengths of dashes and gaps in a stroked line.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
plot_opts = dict(x="x", y="y", width=300, height=200)
(
df.hvplot.line(line_dash="dashed", title="dashed", **plot_opts) +
df.hvplot.line(line_dash="dotted", title="dotted", **plot_opts) +
df.hvplot.line(line_dash="dotdash", title="dotdash", **plot_opts) +
df.hvplot.line(line_dash="dashdot", title="dashdot", **plot_opts) +
df.hvplot.line(line_dash=(20, 10), title="Dash pattern (20, 10)", **plot_opts) +
df.hvplot.line(line_dash=(20, 10, 5), title="Dash pattern (20, 10, 5)", **plot_opts)
).cols(2)
The Matplotlib backend accepts these values for the linestyle
keyword:
One of
'-'
/'solid'
,'--'
/'dashed'
,'-.'
/'dashdot'
,':'
/'dotted'
A dash tuple
(offset, (on_off_seq))
. For example,(0, (3, 10, 1, 15))
means (3pt line, 10pt space, 1pt line, 15pt space) with no offset, while(5, (10, 3))
, means (10pt line, 3pt space), but skip the first 5pt line.
Find more information about Matplotlib linestyles in its documentation.
import hvplot.pandas # noqa
import pandas as pd
hvplot.extension("matplotlib")
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
plot_opts = dict(x="x", y="y", width=300, height=200)
(
df.hvplot.line(linestyle="dashed", title="dashed", **plot_opts) +
df.hvplot.line(linestyle="dotted", title="dotted", **plot_opts) +
df.hvplot.line(linestyle="dashdot", title="dashdot", **plot_opts) +
df.hvplot.line(linestyle=(0, (1, 10)), title="dash tuple (0, (1, 10))", **plot_opts)
).cols(2)
Set the line width#
The line width can be controlled with the line_width
keyword for the Bokeh plotting backend and the linewidth
keyword for the Maplotlib plotting backend.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame({"x": [0, 1, 2, 3], "y": [0, 1, 4, 9]})
df.hvplot.line(x="x", y="x", line_width=10)
Xarray example#
We plot the evolution of the air temperature at a specific lat/lon location.
import hvplot.xarray # noqa
ds = hvplot.sampledata.air_temperature("xarray").sel(lon=285.,lat=40.)
ds.hvplot.line(y="air")