VOOZH about

URL: https://www.geeksforgeeks.org/data-visualization/creating-a-chart-with-two-different-y-axis-ranges-in-bokeh/

⇱ Creating a Chart with Two Different Y-Axis Ranges in Bokeh - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Creating a Chart with Two Different Y-Axis Ranges in Bokeh

Last Updated : 23 Jul, 2025

Bokeh is a powerful visualization library in Python that allows for the creation of interactive plots and dashboards. One of the more advanced features of Bokeh is the ability to create a chart with two different Y-axis ranges. This can be particularly useful when dealing with datasets that have vastly different scales. In this article, we will delve into how to implement this feature in Bokeh, addressing common challenges and providing a step-by-step guide.

Understanding the Need for Dual Y-Axis

In data visualization, there are instances where you need to plot two datasets that share the same X-axis but have different Y-axis ranges. For example, you might want to plot temperature and precipitation on the same chart. Temperature might range from -10 to 40 degrees Celsius, while precipitation could range from 0 to 200 mm. Plotting these on the same Y-axis would make one of the datasets unreadable due to scale differences.

Challenges with Dual Y-Axis

Using a dual Y-axis can introduce complexity in interpreting the chart:

  • The primary challenge is ensuring that the data is not misrepresented due to the scaling of the axes.
  • Additionally, Bokeh's default behavior might not automatically accommodate the dynamic nature of data ranges, especially when the data changes over time or with user interaction.

Creating a Dual Y-Axis Plot using Bokeh

Key Steps in the Implementation:

  • Data Preparation: Use ColumnDataSource to manage the data for each dataset. This allows for efficient updates and interactions.
  • Primary Y-Axis: Plot the first dataset using the default Y-axis.
  • Secondary Y-Axis:
    • Define an additional Y-axis range using Range1d.
    • Add this range to the plot using extra_y_ranges.
    • Attach a new LinearAxis to the plot, specifying the y_range_name.
  • Plotting: Use the line method to plot each dataset, specifying the y_range_name for the second dataset to ensure it uses the secondary Y-axis.

Example 1: Creating a Basic Plot with Dual Y-Axis

Let's start by creating a basic plot in Bokeh. We'll use two datasets with different Y-axis ranges.

Output:

👁 dual-y-axis
Dual Y-axis Plot in Bokeh

Example 2: Dual Y-Axis with Real-World Data

Consider a scenario where you want to plot stock prices and trading volumes on the same chart. Stock prices might range from $100 to $150, while trading volumes could range from 1,000 to 10,000 shares.

Output:

👁 stock
Dual Y-Axis with Real-World Data

Handling Dynamic Data in Bokeh

When dealing with dynamic data where the Y-axis ranges can change, it's crucial to ensure the axes adjust accordingly. Bokeh provides DataRange1d which can automatically adjust based on the data being plotted. This is particularly useful for real-time data visualization.

Output:

👁 dynamic-data
Handling Dynamic Data in Bokeh

Considerations and Best Practices

  • Avoid Misinterpretation: Ensure that the scaling of both Y-axes does not mislead the viewer about the data's relationship.
  • User Interaction: Consider adding tools like zoom and pan to allow users to explore the data more interactively.
  • Alternatives: If dual Y-axes lead to confusion, consider alternative visualizations like facet plots or separate charts.

Conclusion

Creating a chart with two different Y-axis ranges in Bokeh is a powerful way to visualize datasets with varying scales. By carefully managing the axes and ensuring clear labeling, you can create informative and interactive visualizations. While dual Y-axes can be complex, Bokeh's flexibility makes it possible to implement them effectively. Always consider the end-user's ability to interpret the data accurately when designing such visualizations.

Comment