How to set size of mouse coordinates in matplotlib toolbar?

Viewed 548

I want to increase the size of mouse coordinates (Right in the navigation toolbar) mpl.rcParams['font.size'] = 35 change size of text in the plot but not in the Toolbar.Is there a rcParams for this or some other way to set the size. I have spent almost one day trying to solve this.

enter image description here '''

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.size'] = 35
x=np.arange(0,100)
y=np.sin(np.pi*x/10)
plt.plot(x,y)

plt.title('Sin(x)')
plt.show()

'''

2 Answers

I am not sure whether it is possible to change that specific font size. However, as an alternative, it is possible to draw the mouse coordinates using the text method. To this end, I create a figure with two axes. The top axes is the smallest and is used to draw the coordinates of the mouse using the aforementioned text method, while the bottom axes is used for your plot. In addition, I have defined a function which will be called once the mouse moves. This function is responsible for updating the mouse coordinates. See the code below:

import numpy as np
import matplotlib.pyplot as plt

# Set option(s)
plt.rcParams['font.size'] = 15

# Generate data to plot
x = np.arange(0, 100)
y = np.sin(np.pi * x / 10)

# Create a figure with two axes:
# Top axes   :  Used to draw text containing the coordinates of the mouse
# Bottom axes:  Used to plot the data
fig, ax = plt.subplots(nrows=2, 
                       ncols=1, 
                       # Note: default figsize is [6.4, 4.8] in inches
                       figsize=(6.4, 5.6), 
                       # Using height_ratios, we make the top axes small
                       # and the bottom axes as big as usual
                       gridspec_kw={'height_ratios': [0.8 / 5.6, 4.8 / 5.6]}
                       )

# ax[0] is where the coordinates will be shown, so remove the x- and y-axis
ax[0].axis('off')

# Set default text on the top axes
default_text = "Cursor location will be shown here"
ax[0].text(0.5, 0.5, default_text, va='center', ha='center', color='grey')

# Plot the data on the bottom axes (ax[1])
ax[1].plot(x, y)
ax[1].set_title("Sin(x)")

# Define a function which is called when the location of the mouse changes
def update_mouse_coordinates(ax, text, event):
    
    # Don't print coordinates if not on bottom axes
    if event.inaxes == ax or event.xdata==None or event.ydata==None:
        ax.texts[0].set_text(text)
        plt.draw()
        return
    
    # Show mouse coordinates to user
    ax.texts[0].set_text(f"x={event.xdata:.4f}; y={event.ydata:.4f}")
    plt.draw()


plt.connect('motion_notify_event', 
            # Using a lambda expression, we supply the top axes and the default
            # text to this function (our resulting function may only have one
            # argument!)
            lambda event: update_mouse_coordinates(ax[0], default_text, event)
            )

# Show the plot
plt.show()

Whenever the mouse is outside of the bottom axes (where your plot is), it will look as follows: Mouse outside bottom axes

As soon as your mouse is somewhere inside the bottom axes, you will get the following behaviour: Mouse inside bottom axes

This is not a complete answer, but I see no way to put a few lines of code in a comment.

I was able to solve this problem in a Qt GUI application like this:

self.toolbar = NavigationToolbar2QT(self.view, self)
self.toolbar.setStyleSheet("font-size: 12px;")
Related