首页 > 解决方案 > 如何将图表中 x 轴的第一个标签从 0.0 修改为 0?

问题描述

当我有我的图表时,我试图在 x 轴的开头将 0.0 更改为 0。

我的数字数据是:

x = 0.115, 0.234, 0.329, 0.443, 0.536, 0.654, 0.765, 0.846

y = 5.598, 7.6942, 9.1384, 11.2953, 12.4065, 15.736, 21.603, 31.4367

s = 0.05, 0.1, 0.16, 0.4, 0.32, 0.17, 0.09, 1.2

原始数据没有 x = 0, y = 0。我使用命令添加它并自动制作图表。但图表在 x 轴上从 0.0 开始。如何在不影响其余数字的情况下将 0.0 更改为 0?

我研究了以下链接...但仍然没有成功... 修改刻度标签文本 pyplot删除零的数字(从0开始而不是0.00)

我的命令是:

import pandas as pd
import matplotlib.pyplot as plt

datos = pd.read_csv('.name.csv')
print(datos)

datosSM1 = datos[0:0]
datosSM1.loc[0] = 0
datosSM2 = datos[0:]

datosSM = pd.concat([datosSM1, datosSM2])
print(datosSM)

x = datosSM['x']
y = datosSM['y']
ys = datosSM['s']

plt.errorbar(x,y, fmt = 'ko', label = 'datos', 
         yerr = ys, ecolor='r' )
plt.axis([0, x.max()+0.02, 0, y.max()+(y.max()/10)])

plt.show()

我非常感谢您的帮助和关注。

标签: pandasxticks

解决方案


真的非常感谢您,并提出了很好的建议。我认为您的代码比我刚刚编写的替代代码更好...

    ax = plt.axes()
def format_func(value, tick_number):
    N = value
    if N == 0:
        return "0"
    else:
        return "{0:0.2}".format(N)

ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))

谢谢


推荐阅读