To plot multiple lines on a graph using Bokeh in Python, you need to follow these steps:
- Import the necessary libraries: To create a graph using Bokeh, you need to import the necessary libraries, including bokeh.plotting, numpy, and pandas.
import bokeh.plotting as bp import numpy as np import pandas as pd
- Create a data source: Create a data source for the lines you want to plot on the graph. You can use NumPy or Pandas to create the data source.
x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) df = pd.DataFrame({'x': x, 'y1': y1, 'y2': y2})
- Create a figure: Create a figure object using Bokeh’s figure() function.
fig = bp.figure(title='Multiple Lines Example', x_axis_label='X Axis', y_axis_label='Y Axis')
- Plot the lines: Use Bokeh’s line() function to plot the lines on the graph. You can add multiple lines to the same graph by calling the line() function multiple times.
fig.line('x', 'y1', legend_label='Line 1', line_width=2, line_color='blue', source=df) fig.line('x', 'y2', legend_label='Line 2', line_width=2, line_color='red', source=df)
- Add legends and labels: Add legends and labels to the graph to make it easier to read.
fig.legend.location = 'top_left' fig.legend.title = 'Legend Title' bp.show(fig)
- Display the graph: Use Bokeh’s show() function to display the graph.
Here’s the complete code:
import bokeh.plotting as bp import numpy as np import pandas as pd x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) df = pd.DataFrame({'x': x, 'y1': y1, 'y2': y2}) fig = bp.figure(title='Multiple Lines Example', x_axis_label='X Axis', y_axis_label='Y Axis') fig.line('x', 'y1', legend_label='Line 1', line_width=2, line_color='blue', source=df) fig.line('x', 'y2', legend_label='Line 2', line_width=2, line_color='red', source=df) fig.legend.location = 'top_left' fig.legend.title = 'Legend Title' bp.show(fig)
This will create a graph with two lines plotted on it, each with its own legend label.
plotting.figure.multi_line()
plotting.figure.multi_line()
is a method in Bokeh’s figure()
function that is used to plot multiple lines on a graph in a single call. It takes a list of arrays as input, where each array represents the x-coordinates and y-coordinates of one line. The method can be useful when you need to plot a large number of lines on a graph.
Here is the syntax for using plotting.figure.multi_line()
:
from bokeh.plotting import figure, show # create the figure object fig = figure() # create a list of x-coordinates and y-coordinates for each line xs = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] ys = [[6, 7, 2], [4, 5, 6], [7, 2, 3]] # plot the lines using multi_line fig.multi_line(xs, ys, line_color=["blue", "red", "green"], line_width=2) # show the figure show(fig)
In this example, multi_line()
is used to plot three lines with different colors and a line width of 2. The x-coordinates and y-coordinates for each line are specified in the xs
and ys
arrays, respectively.
Note that the xs
and ys
arrays must have the same length and that each element in the arrays should contain the coordinates for a single line. If you need to plot lines with different numbers of points, you can pad the shorter arrays with NaN
values to make them the same length.
Parameters:
The plotting.figure.multi_line()
method has the following parameters:
xs
(required): A list of arrays, where each array represents the x-coordinates of one line. The arrays must have the same length.ys
(required): A list of arrays, where each array represents the y-coordinates of one line. The arrays must have the same length as the corresponding x-coordinates array inxs
.line_color
(optional): A list of colors to use for the lines. If not specified, Bokeh will use its default line colors.line_width
(optional): The width of the lines in pixels. Default is 1.line_alpha
(optional): The opacity of the lines, between 0 (completely transparent) and 1 (completely opaque). Default is 1.line_join
(optional): The type of join to use between line segments. Possible values are “miter”, “round”, and “bevel”. Default is “miter”.line_cap
(optional): The type of cap to use at the ends of lines. Possible values are “butt”, “round”, and “square”. Default is “butt”.line_dash
(optional): The dash pattern to use for the lines. This can be a string of the form “4 4” to specify a repeating pattern of 4 pixel dashes followed by 4 pixel gaps, for example. Default is None, which means no dash pattern is used.line_dash_offset
(optional): The offset into the line dash pattern at which to start the dashes. Default is 0.line_alpha
(optional): The opacity of the lines, between 0 (completely transparent) and 1 (completely opaque). Default is 1.line_color_mapper
(optional): A mapper object that maps scalar data to colors. This can be used to color lines based on a third variable. Default is None.source
(optional): A data source object that provides the data for the lines. If not specified, Bokeh will create a default data source based on thexs
andys
arrays.
Return:
The plotting.figure.multi_line()
method does not return any value. It simply adds the lines to the Figure
object that is created using the figure()
function. To display the figure, you can use the show()
function, passing the Figure
object as an argument. Here is an example:
from bokeh.plotting import figure, show # create the figure object fig = figure() # create a list of x-coordinates and y-coordinates for each line xs = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] ys = [[6, 7, 2], [4, 5, 6], [7, 2, 3]] # plot the lines using multi_line fig.multi_line(xs, ys, line_color=["blue", "red", "green"], line_width=2) # show the figure show(fig)
This will display the figure with the three lines plotted in different colors and with a line width of 2.
Conclusion:
plotting.figure.multi_line()
is a useful method in the Bokeh library for plotting multiple lines on a graph in a single call. It takes a list of arrays as input, where each array represents the x-coordinates and y-coordinates of one line. The method allows you to customize the color, width, and style of the lines, and can be used to plot a large number of lines on a graph.
To display the figure with the lines plotted, you can use the show()
function, passing the Figure
object as an argument. Bokeh provides many other tools and options for customizing the appearance and behavior of the figure, allowing you to create interactive and engaging data visualizations in Python.