pandas - 如何将图表中 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()
我非常感谢您的帮助和关注。
解决方案
真的非常感谢您,并提出了很好的建议。我认为您的代码比我刚刚编写的替代代码更好...
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))
谢谢
推荐阅读
- react-native - 使用 Formik 反应原生日期选择器
- java - 在 Super 类中找不到主要方法
- python - 根据 Pandas 中不同列的 group by 更新特定列值
- javascript - 我如何创建下一页和上一页
- javascript - Dom 元素不适用于 1 个变量,也不适用于“let”变量
- php - 如何在laravel中检查数据数组到where条件?
- node.js - module.exports 与环境变量
- javascript - 我可以从应用程序将 cors 扩展到路由器吗?
- html - 带有箭头图标的角度自定义排序
- reactjs - 对未安装的组件做出反应状态更新。这是一个无操作,但它表明您的应用程序中存在内存泄漏