如何在 x 和 y 轴上用逗号替换点? [复制]
Posted
技术标签:
【中文标题】如何在 x 和 y 轴上用逗号替换点? [复制]【英文标题】:How do I replace the dot with a comma on my x and y-axis? [duplicate] 【发布时间】:2021-03-31 10:12:18 【问题描述】:如何在我的 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()
【问题讨论】:
你试过this吗? 【参考方案1】:(没有语言环境的手动方式)
对于两个轴,我们获取当前刻度标签,然后使用列表推导对其进行替换操作;然后我们把它放回去:
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)
【讨论】:
【参考方案2】:您可以通过导入 locale
并选择使用逗号而不是点作为小数点分隔符的系统来实现(例如意大利语)
import locale
locale.setlocale(locale.LC_NUMERIC, "it_IT")
然后:
plt.rcParams['axes.formatter.use_locale'] = True
【讨论】:
以上是关于如何在 x 和 y 轴上用逗号替换点? [复制]的主要内容,如果未能解决你的问题,请参考以下文章