简体   繁体   English

如何强制绘图在python中显示x轴值

[英]How to force the plot to show the x-axis values in python

I have an issue making a simple plot while setting the x-axis in python. 在设置python中的x轴时,制作一个简单的绘图时遇到问题。 Here is my code: 这是我的代码:

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()

I believe my code is right! 我相信我的代码是正确的! Notice the line of code I commented, it should set the axis with the values I want, however, it throws an error. 请注意我注释的代码行,它应将轴设置为所需的值,但是会引发错误。 I cannot figure out why! 我不知道为什么!

Here is my plot from the my plot: 这是我的情节中的我的情节:

在此处输入图片说明

I used the following and it ran without errors. 我使用了以下内容,并且没有错误地运行。

All I changed is the typo in your first line of your imports, and replaced loss_ave_hist with y (ie what you called your data in your question. 我所更改的只是输入的第一行中的错字,并用y替换了loss_ave_hist (即,您在问题中称数据为什么)。

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()

图片在这里

UPDATE : I understand you want to label the x-axis with values from 0..29, but on a log scale, all those numbers are very close. 更新 :我知道您想用0..29的值标记x轴,但是在对数刻度上,所有这些数字都非常接近。

Here is an image with xticks set (I din't get any errors): 这是设置了xticks的图像(我没有得到任何错误):

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()

IMG

Here is an image where I replace semilogx with semilogy . 这里是我更换的图像semilogxsemilogy

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()

Does any of this resemble your goal? 这与您的目标类似吗?

Here is a way to make a semilogx plot but with xticks labelled according to their original (non-log) values. 这是一种制作Semilogx图的方法,但是xticks根据其原始(非对数)值进行标记。

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()

yields 产量 在此处输入图片说明 FixedLocator(np.arange(0, x.max()+1, 5))) places a tick mark at every 5th value in x . FixedLocator(np.arange(0, x.max()+1, 5)))x第5个值处放置一个刻度线。 With ax.xaxis.set_major_locator(mticker.FixedLocator(x)) , the xticklabels got a bit too crowded. 使用ax.xaxis.set_major_locator(mticker.FixedLocator(x)) ,xtick标签有点拥挤。


Note I changed x = range(0, 30) to x = np.arange(1, len(y)+1) since the length of x should match the length of y and since we are using a logarithmic x -axis, it does not make sense to start at x=0 . 注意我将x = range(0, 30) x = np.arange(1, len(y)+1) x = range(0, 30)更改为x = np.arange(1, len(y)+1)因为x的长度应与y的长度匹配,并且因为我们使用对数的x轴,所以它从x=0开始是没有意义的。

Notice also that in your original code the first y value (2586.08...) is missing since its associated x value, 0, is off-the-chart on a logarithmic scale. 还要注意,在您的原始代码中,缺少第一个y值(2586.08 ...),因为其关联的x值0处于对数刻度上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM