wordpress

How to Plot Multiple Lines on a Graph Using Bokeh in Python – Syntax & Examples

1. Importing the necessary libraries

In order to plot multiple lines on a graph using Bokeh in Python, we first need to import the necessary libraries. Bokeh is a powerful data visualization library that provides a flexible and interactive way to create plots. We will also need the NumPy library for generating the data for our lines.

The following code snippet shows how to import the required libraries:

import numpy as np
from bokeh.plotting import figure, show

2. Creating the figure and specifying the plot properties

Next, we need to create a figure object and specify the properties of our plot. The figure object is the main component of a Bokeh plot and it represents the overall canvas or plotting area.

We can specify various properties of the plot, such as the title, x-axis label, y-axis label, and the size of the plot. The following code snippet demonstrates how to create a figure object and specify the plot properties:

plot = figure(title="Multiple Lines Plot", x_axis_label="X-axis", y_axis_label="Y-axis", plot_width=800, plot_height=400)

3. Defining the data for each line

Now, we need to define the data for each line that we want to plot. We can use the NumPy library to generate the data for our lines. For example, let’s say we want to plot three lines with different slopes:

x = np.linspace(0, 10, 100)
y1 = 2 * x
y2 = 3 * x
y3 = 4 * x

In this example, we generate 100 equally spaced points between 0 and 10 for the x-axis. Then, we calculate the corresponding y-values for each line using different slopes.

Recomendado:  Import Export to Excel and CSV using Maatwebsite in Laravel 5 - Step-by-Step Guide

4. Plotting the lines

Once we have defined the data for each line, we can plot them on the graph using the figure object. We can use the line() method of the figure object to plot each line.

The following code snippet demonstrates how to plot the lines:

plot.line(x, y1, legend_label="Line 1", line_color="red")
plot.line(x, y2, legend_label="Line 2", line_color="blue")
plot.line(x, y3, legend_label="Line 3", line_color="green")

In this example, we use the line() method three times to plot each line. We specify the x-values, y-values, legend label, and line color for each line. The legend label is used to identify each line in the legend.

5. Customizing the plot

We can customize various aspects of the plot, such as the line width, line style, and marker style. Bokeh provides a wide range of options to customize the appearance of the plot.

The following code snippet demonstrates how to customize the plot:

plot.line(x, y1, legend_label="Line 1", line_color="red", line_width=2, line_dash="dashed")
plot.line(x, y2, legend_label="Line 2", line_color="blue", line_width=2, line_dash="dotted")
plot.line(x, y3, legend_label="Line 3", line_color="green", line_width=2, line_dash="solid")

In this example, we use the line_width parameter to set the width of each line, the line_dash parameter to specify the line style, and the line_color parameter to set the color of each line.

6. Adding a legend

We can add a legend to our plot to provide a visual representation of each line. The legend helps in identifying the different lines in the plot.

The following code snippet demonstrates how to add a legend to the plot:

plot.legend.location = "top_left"
plot.legend.title = "Lines"
plot.legend.label_text_font_size = "10pt"

In this example, we set the location of the legend to the top left corner of the plot using the legend.location parameter. We also set the title of the legend using the legend.title parameter and the font size of the legend labels using the legend.label_text_font_size parameter.

Recomendado:  SNMP module in Python: Top Python Libraries for SNMP

7. Displaying the plot

Finally, we can display the plot by calling the show() function. This will open a new browser tab and display the plot.

The following code snippet demonstrates how to display the plot:

show(plot)

After executing this code, a new browser tab will open displaying the plot with multiple lines.

That’s it! You have learned how to plot multiple lines on a graph using Bokeh in Python. You can now customize the plot properties, add legends, and display the plot to visualize your data effectively.

Author

osceda@hotmail.com

Leave a comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *