Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diagonal line with Holoviews (Python)

In Holoviews you can draw a horizontal line with hv.HLine() and a vertical line with hv.VLine() and add this to your plot.
But how can I draw a diagonal line and add that to my plot?

like image 790
Sander van den Oord Avatar asked Sep 02 '25 05:09

Sander van den Oord


1 Answers

EDIT: NEW SOLUTION

The simplest way to draw a line, be it a diagonal, or a line with any other slope, is like this, where you give the starting coordinates and ending coordinates:

hv.Curve([[0, 0], [10, 10]]).opts(line_dash='dashed', color='black')

Another option to add a line to an existing plot, is to use hv.Slope(), which requires you to specify a slope and an intercept:

existing_plot = hv.Curve([[0, 2], [10, 10]])
existing_plot * hv.Slope(slope=1, y_intercept=3).opts(color='red')

ALSO A GOOD SOLUTION, BUT MORE COMPLICATED:

The key to get a diagonal line is to get an array of equal coordinates, such as (0,0), (1,1), (2,2). Then use hv.Curve() to draw these coordinates as a diagonal, like so:

# import libraries
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)

# create coordinates such as (0,0), (1,1), (2,2) in an array
numbers = np.arange(0, 10).reshape(-1, 1)
numbers_coordinates = np.concatenate((numbers, numbers), axis=1)

# create the diagonal line or curve
diagonal_line = hv.Curve(numbers_coordinates, label='Plotting diagonal line')

# change the look of your line
diagonal_line = diagonal_line.opts(line_dash='dashed', color='black')

# plot your diagonal line
diagonal_line

Plotting diagonal line

like image 179
Sander van den Oord Avatar answered Sep 04 '25 19:09

Sander van den Oord