1

How do I replace the dot with a comma on my x and y-axis?

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import matplotlib.ticker as mtick

'#De tijd tussen de tijd die gemeten is met de telefoons in seconden (s)'
m1 = [0.0050/2, 0.0140/2, 0.0190/2, 0.0230/2, 0.0280/2]
m2 = [0.0080/2, 0.0110/2, 0.0150/2, 0.0230/2, 0.0280/2]
m3 = [0.0070/2, 0.0100/2, 0.0200/2, 0.0220/2, 0.0290/2]
m4 = [0.0060/2, 0.0120/2, 0.0180/2, 0.0240/2, 0.0280/2]
m5 = [0.0070/2, 0.0110/2, 0.0170/2, 0.0250/2, 0.0310/2]

afstand = np.array([1.0, 2.0, 3.0, 4.0, 5.0])

t_1m = [0.0050, 0.0080, 0.0070, 0.0080, 0.0060, 0.0070]
t_2m = [0.0140, 0.0110, 0.0100, 0.0100, 0.0120, 0.0110]
t_3m = [0.0190, 0.0150, 0.0200, 0.0170, 0.0180, 0.0170]
t_4m = [0.0230, 0.0230, 0.0220, 0.0250, 0.0240, 0.0250]
t_5m = [0.0280, 0.0280, 0.0290, 0.0280, 0.0280, 0.0310]


Onn = []
def onnauwkeurigheid(m):
    """Onnauwkeurigheid berekenen"""
    std1 = np.std(m, ddof=1)
    squared = std1/np.sqrt(len(m))
    keer3 = squared*3
    return Onn.append(keer3)

test1 = onnauwkeurigheid(t_1m)
test2 = onnauwkeurigheid(t_2m)
test3 = onnauwkeurigheid(t_3m)
test4 = onnauwkeurigheid(t_4m)
test5 = onnauwkeurigheid(t_5m)

tijd = (1/343)*afstand

sns.set_style("darkgrid")

plt.errorbar(afstand, m1, xerr= None, yerr= Onn, fmt='+', mec= 'red',
             markersize = 12, capsize = 3, ecolor='red', label='Meting1')
plt.errorbar(afstand, m2, xerr= None, yerr= Onn, fmt='x', mec= 'green',
             markersize = 12, capsize = 3, ecolor='green', label='Meting2')
plt.plot(afstand, tijd, '--', label = 'Theorie')

plt.xlabel('Afstand in $m$')
plt.ylabel('Tijd in $s$')
plt.legend()

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376

2 Answers2

2

(A manual way without locale)

For the two axes, we get the current tick labels and then run a replacing operation on it with a list comprehension; and then we set it back:

ax = plt.gca()  # get the current axis

# x-axis
new_x_tick_labels = [label.get_text().replace(".", ",")
                     for label in ax.xaxis.get_ticklabels()]

# y-axis
new_y_tick_labels = [label.get_text().replace(".", ",")
                     for label in ax.yaxis.get_ticklabels()]

# now set back
ax.set_xticklabels(new_x_tick_labels)
ax.set_yticklabels(new_y_tick_labels)

enter image description here

Mustafa Aydın
  • 1
  • 3
  • 12
  • 32
1

you can do it by import locale and choosing a system that use comma instead of dot as decimal separator (for example the Italian)

import locale
locale.setlocale(locale.LC_NUMERIC, "it_IT")

and then:

plt.rcParams['axes.formatter.use_locale'] = True

enter image description here

Giovanni Frison
  • 623
  • 3
  • 18