如何用我的 x 轴和 y 轴上的逗号替换点?

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

如何用逗号替换 x 轴和 y 轴上的点?

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()

您可以通过导入 locale 并选择使用逗号而不是点作为小数点分隔符的系统(例如意大利语)

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

然后:

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

(无语言环境的手动方式)

对于两个轴,我们得到当前的刻度标签,然后运行用列表理解对其进行替换操作;然后我们将其设置回去:

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)