Simple Example
How can I get the '$' symbol on y-ticks (see red circle), but the '£' symbol on x-ticks?
Here's my code so far:
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
x = np.arange(0,200, 10) # GBP (0, 10, ... 190, 200)
y = x * 1.25 # USD
# Make figure
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y)
ax.set_xlabel('GBP')
ax.set_ylabel('USD')
# How to format tick labels
def format_currency(value, pos):
currency = '£' # <- hard-coded (for now) - but how to include as argument?
return f'{currency}{value:.0f}' # <- e.g., "$100" or "£100"
# Format tick labels
ax.xaxis.set_major_formatter(plt.FuncFormatter(format_currency))
ax.yaxis.set_major_formatter(plt.FuncFormatter(format_currency)) # <- how to add '$' as an argument?
What I'm aiming for?
Ideally, I'd like to pass the currency symbol as an argument into the format_currency() function. That way, it gives me the flexibility to add a '$' symbol onto the y-tick labels, i.e.:
def format_currency(value, pos, currency):
return f'{currency}{value:.0f}'
But then (even assuming that can be done), how would the following line change?
ax.yaxis.set_major_formatter(plt.FuncFormatter(format_currency))
What I've considered so far
- Sure, I could make tailored functions, e.g.,
format_USD(),format_EUR(), etc., but that's not scalable with so many currencies. - I've been toying around with
*argsbut I can't find a way of making it work.
