1. Introduction to Bokeh
Bokeh is a popular Python data visualization library that can generate high-quality interactive charts. Whether it is simple line charts, scatter charts, complex multi-dimensional surface charts, and network charts, Bokeh can easily handle them. At the same time, it supports web output, making the charts easy to share and display. Bokeh’s main goal is to provide excellent interactive visualization solutions for big data and real-time data streams.
2. Installation and basic use
Installing Bokeh with pip is very simple, just run the following command in the terminal:
pip install bokeh
After the installation is complete, we can use Bokeh. Here is a simple example that demonstrates how to create a simple line diagram using Bokeh:
from import figure, show # Prepare datax = [1, 2, 3, 4, 5] y = [6, 7, 2, 3, 6] # Create a new diagramp = figure(title="Simple Line Chart", x_axis_label='x', y_axis_label='y') # Add a line to the graph(x, y, legend_label="Temp.", line_width=2) # Show picturesshow(p)
In this example, we first import the figure and show functions from the module. Then, we prepared some data, created a new graph, added a line to the graph, and finally used the show function to display the graph.
3. Add interactivity
What makes Bokeh powerful is that it supports rich interactive features. For example, we can add interactive components such as toolbars and sliders so that users can control how the chart is displayed. Here is a simple example showing how to add a slider and a callback function to change the data of the chart:
from import curdoc from import column from import ColumnDataSource, Slider from import figure # Create a new ColumnDataSource, which will allow us to change the datasource = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[6, 7, 2, 3, 6])) # Create a new diagramp = figure(title="Interactive Line Chart", x_axis_label='x', y_axis_label='y') ('x', 'y', source=source, legend_label="Temp.", line_width=2) # Create a sliderslider = Slider(start=0, end=10, value=1, step=.1, title="power") # Create a callback function, which will be called when the value of the slider changesdef update_data(attrname, old, new): c = = dict(x=[1, 2, 3, 4, 5], y=[6*c, 7*c, 2*c, 3*c, 6*c]) # Add the callback function to the sliderslider.on_change('value', update_data) # Put the graph and slider into a layoutlayout = column(slider, p) # Add layout to the current documentcurdoc().add_root(layout)
In this example, we first create a new ColumnDataSource, which will store our data and allow us to change it. We then create a new graph and add a line to this graph. Note that when we add lines, we specified the data source for the ColumnDataSource we created earlier.
Next, we create a slider, and a callback function. This callback function is called when the value of the slider changes, and in this function we change the data in the data source. Then, we add this callback function to the slider.
Finally, we created a new layout, put the slider and graph into this layout, and added it to the current document.
4. Add other visual elements
In addition to basic line diagrams and interactive elements, Bokeh also provides many other visual elements such as rectangles, ellipses, polygons, line segments, wedges, sectors, and more. You can add these elements through various methods of the figure object. Here is an example showing how to add some such elements:
from import figure, show # Create a new diagramp = figure(width=400, height=400) # Add some rectangles(x=[1, 2, 3], y=[1, 2, 3], width=0.2, height=40, color="#CAB2D6") # Add some circles(x=[1, 2, 3], y=[1, 2, 3], size=20, color="#FF7F00") # Show picturesshow(p)
In this example, we first create a new graph. Then we add some rectangles and circles through the rect method and the circle method of the figure object. Finally, we use the show function to display this graph.
By using the various features of Bokeh, you can create rich and interactive charts. The above is just the tip of its function, and more functions are waiting for you to explore.
This is the article about Python using Bokeh to create interactive charts. For more related content of Python Bokeh interactive charts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!