如何将图形中 x 轴的第一个标签从 0.0 修改为 0?
How do I modify the first label from 0.0 to 0 of the x axis in my graph?
当我有我的图表时,我曾尝试将 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?
我研究了下面的链接……还是没成功……
Modify tick label text
我的命令是:
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()
非常感谢您的帮助和关注。
要修改选定的标签(实际上是其文本),请尝试以下代码:
# Prepend first row with zeroes
datosSM = pd.concat([pd.DataFrame({'x': 0, 'y': 0, 's': 0}, index=[0]),
datos], ignore_index=True)
# Drawing
fig, ax = plt.subplots() # Will be needed soon
plt.errorbar(datosSM.x, datosSM.y, yerr=datosSM.x, fmt='ko', label='datos', ecolor='r')
plt.axis([0, datosSM.x.max() + 0.02, 0, datosSM.y.max() + (datosSM.y.max() / 10)])
fig.canvas.draw() # Needed to get access to label texts
# Get label texts
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[0] = '0' # Modify the selected label
ax.set_xticklabels(labels)
plt.show()
以上代码的一个额外改进是一种更简洁的方法
生成一个带有零前缀行的数据框。
另一个改进是您不需要 "extract" 单独的列。
您可以传递 DataFrame 的 existing 列。
结果是:
真的非常感谢,还有很好的建议。
我认为您的代码比我刚刚编写的替代代码更好...
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))
谢谢
当我有我的图表时,我曾尝试将 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?
我研究了下面的链接……还是没成功……
Modify tick label text
我的命令是:
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()
非常感谢您的帮助和关注。
要修改选定的标签(实际上是其文本),请尝试以下代码:
# Prepend first row with zeroes
datosSM = pd.concat([pd.DataFrame({'x': 0, 'y': 0, 's': 0}, index=[0]),
datos], ignore_index=True)
# Drawing
fig, ax = plt.subplots() # Will be needed soon
plt.errorbar(datosSM.x, datosSM.y, yerr=datosSM.x, fmt='ko', label='datos', ecolor='r')
plt.axis([0, datosSM.x.max() + 0.02, 0, datosSM.y.max() + (datosSM.y.max() / 10)])
fig.canvas.draw() # Needed to get access to label texts
# Get label texts
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[0] = '0' # Modify the selected label
ax.set_xticklabels(labels)
plt.show()
以上代码的一个额外改进是一种更简洁的方法 生成一个带有零前缀行的数据框。
另一个改进是您不需要 "extract" 单独的列。 您可以传递 DataFrame 的 existing 列。
结果是:
真的非常感谢,还有很好的建议。 我认为您的代码比我刚刚编写的替代代码更好...
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))
谢谢