首页 > 解决方案 > 如何在我的 x 和 y 轴上用逗号替换点?

问题描述

如何在我的 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()

标签: pythonreplaceaxiscommadot

解决方案


(没有语言环境的手动方式)

对于两个轴,我们获取当前刻度标签,然后使用列表推导对其进行替换操作;然后我们把它放回去:

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)

在此处输入图像描述


推荐阅读