如何在python中显示xaxis的值

2024-04-20 06:46:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中设置x轴时,我在绘制简单的绘图时遇到了一个问题。 这是我的代码:

import import matplotlib.pyplot as plt

y  = [2586.087776040828,2285.8044466570227,1991.0556336526986,1719.7261325405243,1479.8272625661773,1272.5176077500348,1096.4367842436593,949.02201512882527,826.89866676342137,726.37921828890637,636.07392349697909,553.52559247838076,480.71257022562935,418.00424110010181,364.41801903538288,318.67575156686001,280.17668207838426,248.15399589447813,221.75070551820284,199.59983992701842,179.72014852370447,162.27141772637697,147.14507926321306,134.22828323366301,123.36572367962557,114.33589702168332,106.8825327470323,100.69181027167537,95.515144406404971,91.091036326792434]
x = range(0,30)

fig3_4 ,ax3_4 = plt.subplots()
ax3_4.semilogx(range(0,30),(loss_ave_hist))
ax3_4.set_title('Validation Performance')
# ax3_4.set_xticks(np.arange(0,30, 1.0))
ax3_4.set_xlabel('i')
ax3_4.set_ylabel('Average Loss')
fig3_4.show()
plt.show()

我相信我的密码是正确的!注意我注释的代码行,它应该设置 但是,带有我想要的值的轴抛出了一个错误。我想不通 为什么!在

以下是我的情节:

enter image description here


Tags: 代码import绘图matplotlibasshow绘制range
2条回答

我使用了下面的代码,运行时没有出错。在

我所改变的只是导入第一行中的错误,并将loss_ave_hist替换为y(即,您在问题中所称的数据)。在

y  = [2586.087776040828,2285.8044466570227,1991.0556336526986,1719.7261325405243,1479.8272625661773,1272.5176077500348,1096.4367842436593,949.02201512882527,826.89866676342137,726.37921828890637,636.07392349697909,553.52559247838076,480.71257022562935,418.00424110010181,364.41801903538288,318.67575156686001,280.17668207838426,248.15399589447813,221.75070551820284,199.59983992701842,179.72014852370447,162.27141772637697,147.14507926321306,134.22828323366301,123.36572367962557,114.33589702168332,106.8825327470323,100.69181027167537,95.515144406404971,91.091036326792434]   

import matplotlib.pyplot as plt
fig3_4 ,ax3_4 = plt.subplots()
x = range(0,30)
ax3_4.semilogx(range(0,30),(y))
ax3_4.set_title('Validation Performance')
# ax3_4.set_xticks(np.arange(0,30, 1.0))
ax3_4.set_xlabel('i')
ax3_4.set_ylabel('Average Loss')
plt.show()

Image here

更新:我知道您希望用0到29之间的值来标记x轴,但是在对数刻度上,所有这些数字都非常接近。在

这是一个设置了xticks的图像(我没有得到任何错误):

^{pr2}$

img

这是一张图片,我将semilogx替换为semilogy。在

fig3_4 ,ax3_4 = plt.subplots()
x = range(0,30)
ax3_4.semilogy(range(0,30),(y))
ax3_4.set_title('Validation Performance')
ax3_4.set_xticks(np.arange(0,30, 1.0))
ax3_4.set_xlabel('i')
ax3_4.set_ylabel('Average Loss')
plt.show()

这和你的目标相似吗?在

这是一种制作semilogx图的方法,但是xtick根据其原始(非log)值进行标记。在

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

y = np.array([2586.087776040828, 2285.8044466570227, 1991.0556336526986, 1719.7261325405243, 1479.8272625661773, 1272.5176077500348, 1096.4367842436593, 949.02201512882527, 826.89866676342137, 726.37921828890637, 636.07392349697909, 553.52559247838076, 480.71257022562935, 418.00424110010181, 364.41801903538288, 318.67575156686001, 280.17668207838426, 248.15399589447813, 221.75070551820284, 199.59983992701842, 179.72014852370447, 162.27141772637697, 147.14507926321306, 134.22828323366301, 123.36572367962557, 114.33589702168332, 106.8825327470323, 100.69181027167537, 95.515144406404971, 91.091036326792434])
x = np.arange(1, len(y)+1)

fig, ax = plt.subplots()
ax.plot(x, y, 'o-')
ax.set_xlim(x.min(), x.max())
ax.set_xscale('log')
formatter = mticker.ScalarFormatter()
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_major_locator(mticker.FixedLocator(np.arange(0, x.max()+1, 5)))
plt.show()

收益率 enter image description hereFixedLocator(np.arange(0, x.max()+1, 5)))x中每隔5个值放置一个刻度线。 使用ax.xaxis.set_major_locator(mticker.FixedLocator(x)),xticklabels变得有点拥挤。在


注:我将x = range(0, 30)改为x = np.arange(1, len(y)+1)x的长度应该与y的长度相匹配,因为我们使用的是对数x-轴,所以从x=0开始是没有意义的。在

还请注意,在您的原始代码中,第一个y值(2586.08…)丢失了,因为其关联的x值0在对数刻度上偏离了图表。在

相关问题 更多 >