Tick_formatter_multiples_of() Label Format Enhancement
I am writing to discuss potential improvements to the label format within the tick_formatter_multiples_of() function in the spb plotting library. First, I want to express my appreciation for the introduction of tick_formatter_multiples_of() in version 4 of spb. It’s a valuable tool, particularly when visualizing trigonometric functions. This article outlines some suggestions to enhance its functionality, focusing on making the label formatting more versatile and user-friendly.
Expanding Label Support for Enhanced Flexibility
Currently, the label parameter in tick_formatter_multiples_of() exclusively accepts LaTeX-formatted symbols without the surrounding $ delimiters. While this is functional for many mathematical expressions, it limits the ability to use general Unicode characters directly within the labels. To enhance the flexibility and usability of this function, it would be beneficial to extend the label parameter to accept general Unicode characters. This enhancement would align the function's capabilities with other parts of the library, such as the line() function, which already supports Unicode characters in labels.
The Benefits of Unicode Support
Supporting Unicode characters directly in labels would significantly improve the user experience by allowing for a broader range of symbols and characters to be used without the complexities of LaTeX formatting. This is particularly useful for labels that include non-mathematical text or symbols not easily represented in LaTeX. For instance, users might want to include degree symbols (°), subscripts, superscripts, or other special characters in their plot labels. By allowing Unicode, the function becomes more intuitive and accessible to users who may not be familiar with LaTeX or who prefer a more straightforward approach to labeling.
Consistency with Other Library Functions
Many functions within plotting libraries already support Unicode characters in labels, providing a consistent experience for users. Extending this support to tick_formatter_multiples_of() would align it with these other functions, making the library as a whole more cohesive and user-friendly. For example, the line() function in spb allows users to include Unicode characters directly in the labels, which simplifies the process of adding descriptive text to plots. By adopting a similar approach for tick_formatter_multiples_of(), the library can offer a more unified and intuitive interface.
Practical Examples of Unicode Usage
To illustrate the benefits of Unicode support, consider a scenario where you want to label the y-axis ticks with degree symbols to indicate angles in degrees. Currently, this would require using LaTeX to represent the degree symbol, which can be cumbersome. With Unicode support, you could directly include the degree symbol in the label string, making the code cleaner and easier to read. Similarly, if you need to include subscripts or superscripts in your labels, Unicode support would allow you to do so without resorting to LaTeX commands. This directness enhances the readability and maintainability of your code.
Enhancing Tick Formatting for Numerical Axes
Another area for potential improvement is the formatting of numerical axes using tick_formatter_multiples_of(). Currently, the function does not produce the desired output when the label parameter is set to an empty string ('') and the quantity parameter is set to 1. In this scenario, the expectation is that the function would generate ticks with integer values, such as ..., -4, -3, -2, -1, 0, 1, 2, 3, 4, ..., but this is not the current behavior. Enhancing the function to support this use case would make it more versatile and useful for a broader range of plotting scenarios.
The Need for Clear Numerical Tick Labels
In many scientific and engineering plots, it is essential to have clear and easily readable numerical tick labels. This is particularly true for axes that represent physical quantities, such as temperature, pressure, or time. When plotting data against these quantities, it is often desirable to have tick marks at regular integer intervals to facilitate quick and accurate reading of the plot. The current behavior of tick_formatter_multiples_of() makes it challenging to achieve this, as it requires the user to specify a label even when a simple numerical format is desired.
Suggested Behavior for Empty Labels
To address this limitation, I propose that when the label parameter is set to an empty string and the quantity is set to 1, the function should generate tick labels that are simply the integer multiples of the specified quantity. For example, if the axis range is from -5 to 5, the tick labels would be ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, .... This behavior would align with the expectations of many users and make the function more intuitive to use for basic numerical formatting. It would also reduce the amount of code required to generate simple numerical tick labels, as users would not need to provide a dummy label or resort to other formatting methods.
Practical Use Cases
Consider a scenario where you are plotting a time series dataset, and you want the x-axis to display tick marks at integer time intervals. With the proposed enhancement, you could easily achieve this by setting the quantity to 1 and the label to an empty string. This would result in clean and readable tick labels that clearly indicate the time points in your dataset. Similarly, if you are plotting a function over a range of integer values, this enhancement would allow you to easily display the integer values as tick labels on the axis. These examples illustrate the practical benefits of this improvement in making numerical formatting more straightforward.
Code Examples and Demonstrations
To illustrate these points, let's consider the provided code examples and how they could be improved with the suggested enhancements.
Example 1: Trigonometric Functions
The original example demonstrates the use of tick_formatter_multiples_of() with LaTeX-formatted symbols for labeling multiples of π on the x-axis. The code works well in this specific case, but it highlights the limitation of only accepting LaTeX-formatted symbols.
from sympy.abc import *
from sympy import *
from spb import *
# It works. 'label' only accepts the latex-formatted symbol
# without $ $
xtf = tick_formatter_multiples_of(quantity=pi, label='\\pi', n=2)
# It would be better to extend so that 'label' accepts
# general (unicode) characters. For latex-formatted symbols,
# in the same way as, e.g., line(... ),
# xtf = tick_formatter_multiples_of(quantity=pi, label='$\pi{{content}}#39;, n=2)
# The following does not work now.
# ytf = tick_formatter_multiples_of(quantity=1, label='', n=1)
# It would be nicer if the above gives
# ticks like ..., -4, -3, -2, -1, 0, 1, 2, 3, -4, ..
graphics(
line(sin(x), label='$\sin x{{content}}#39;),
line(cos(x), label='$\cos x{{content}}#39;),
line(tan(x), label='$\tan x{{content}}#39;, detect_poles=True),
x_ticks_formatter=xtf,
# y_ticks_formatter = ytf,
xlim=(-2 * pi, 2 * pi + 1), ylim=(-5, 5),
);
With the proposed Unicode support, the commented-out line xtf = tick_formatter_multiples_of(quantity=pi, label='$\pi