Python 在x轴上绘制一些数字
我的问题与使用matplotlib库生成的地物格式有关 因此,在x轴上,我有从1到100的整数,但在我的代码中,我只绘制与一些数字相关的数据(例如1,5,77,86,97) 最后,我的代码向我显示了一个x轴从1到100的图形 如何显示所需的数字(1,5,77,86,97) 我的代码如下所示:Python 在x轴上绘制一些数字,python,matplotlib,Python,Matplotlib,我的问题与使用matplotlib库生成的地物格式有关 因此,在x轴上,我有从1到100的整数,但在我的代码中,我只绘制与一些数字相关的数据(例如1,5,77,86,97) 最后,我的代码向我显示了一个x轴从1到100的图形 如何显示所需的数字(1,5,77,86,97) 我的代码如下所示: def Plot(self,x,y, steps, nb,bool): if(bool == False): labelfont = { 'fa
def Plot(self,x,y, steps, nb,bool):
if(bool == False):
labelfont = {
'family' : 'sans-serif', # (cursive, fantasy, monospace, serif)
'color' : 'black', # html hex or colour name
'weight' : 'normal', # (normal, bold, bolder, lighter)
'size' : 20, # default value:12
}
titlefont = {
'family' : 'serif',
'color' : 'black',
'weight' : 'bold',
'size' : 20,
}
x = np.array(x)
y = np.array(y)
pylab.plot(x, y,
'darkmagenta', # colour
linestyle='', # line style
linewidth=10, # line width
marker = '+',
mew=6, ms=12)
axes = plt.gca()
axes.grid(True)
axes.set_xlim([1, x_max) # x-axis bounds
axes.set_ylim([1, nb]) # y-axis bounds
pylab.title('Title' , fontdict=titlefont)
pylab.xlabel('x', fontdict=labelfont)
pylab.ylabel('y', fontdict=labelfont)
pylab.subplots_adjust(left=0.15) # prevents overlapping of the y label
else:
test_steps.insert(0,"")
pylab.yticks(nb, steps,rotation=0, size=10)
pylab.margins(0.2)
pylab.xticks(np.arange(0, x_max, 1.0),rotation=90, size=10)
# Tweak spacing to prevent clipping of tick-labels
pylab.subplots_adjust(bottom=0.15)
F = pylab.gcf()
DefaultSize = F.get_size_inches()
if x_max < 100 and len(steps) < 100:
Xx = 2.5
Yy = 2.5
elif x_max < 200 and len(steps) < 200:
Xx = 5
Yy = 5
elif ix_max < 300 and len(steps) < 300:
Xx = 8
Yy = 8
elif x_max < 400 and steps < 400:
Xx = 10
Yy = 10
elif x_max < 500 and steps < 500:
Xx = 12
Yy = 12
else:
Xx = 15
Yy = 15
F.set_size_inches((DefaultSize[0]*Xx , DefaultSize[1]*Yy))
F.savefig('Out.png', bbox_inches='tight')
pylab.close(F)
def图(self、x、y、steps、nb、bool):
如果(bool==False):
拉贝尔丰={
“家庭”:“无衬线”,“#(草书、幻想、单空格、衬线)
“颜色”:黑色、#html十六进制或颜色名称
‘重量’:‘正常’、#(正常、粗体、粗体、较轻)
“大小”:20,#默认值:12
}
标题字体={
“家庭”:“serif”,
“颜色”:“黑色”,
“重量”:“粗体”,
“尺寸”:20,
}
x=np.数组(x)
y=np.数组(y)
派拉布图(x,y,
“黑金塔”,颜色
线条样式=“”,#线条样式
线宽=10,#线宽
标记=“+”,
mew=6,ms=12)
轴=plt.gca()
轴网格(真)
轴。设置_xlim([1,x_max)#x轴边界
轴。设置y轴界限([1,nb])
pylab.title('title',fontdict=titlefont)
pylab.xlabel('x',fontdict=labelfont)
pylab.ylabel('y',fontdict=labelfont)
pylab.子批次_调整(左=0.15)#防止y标签重叠
其他:
测试步骤。插入(0,“”)
pylab.yticks(注意,台阶,旋转=0,尺寸=10)
pylab.利润率(0.2)
pylab.xticks(np.arange(0,x_max,1.0),旋转=90,大小=10)
#调整间距以防止剪切记号标签
pylab.子批次调整(底部=0.15)
F=pylab.gcf()
DefaultSize=F.获取大小英寸()
如果x_max<100且len(步数)<100:
Xx=2.5
Yy=2.5
elif x_max<200和len(步数)<200:
Xx=5
Yy=5
elif ix_max<300和len(步数)<300:
Xx=8
Yy=8
elif x_最大值<400且步数<400:
Xx=10
Yy=10
elif x_max<500且步数<500:
Xx=12
Yy=12
其他:
Xx=15
Yy=15
F.设置尺寸英寸((默认尺寸[0]*Xx,默认尺寸[1]*Yy))
F.savefig('Out.png',bbox_英寸='tight')
pylab.关闭(F)
如果您有matplotlib.Axes
对象,则可以使用set_xticks
沿x轴修改记号:
import matplotlib.pyplot as plt
plt.plot([0,1,5,77,86,97],range(6),'o-')
ax = plt.gca()
ax.set_xticks([1,5,77,86,97])
输出:
编辑:假设您希望以相等的间隔绘制值(尽管此时您确实应该考虑使用数字作为标签的条形图),您可以制作一个假x轴,并使用setxticklabels
重命名刻度:
import matplotlib.pyplot as plt
x = range(5)
y = range(5)
real_x = [1,5,77,86,97]
plt.plot(x,y,'o-')
ax = plt.gca()
ax.set_xticks(x)
ax.set_xticklabels(real_x)
用y屏蔽x,然后绘制它怎么样? 比如: 或者,如果y包含更多的数字!=0,而您只想绘制上面列出的数字,只需使用这些数字遮罩x…即可:
import matplotlib.pyplot as plt
x = y = list(range(100))
idx = [1,5,77,86,97]
new_y = [y[i] for i in idx]
plt.plot(range(len(idx)), new_y, 'o-')
plt.xticks(range(len(idx)),idx)
plt.show()
如果只需要点,只需删除-
:
plt.plot(range(len(idx)), new_y, 'o')
# ---^--- instead of 'o-'
请发布您的代码。@DavidG:我编辑了我的帖子。我的意思是1和5、5和77、77和86之间的距离应该是相同的。@StackOver所以您想要的是,即使它们之间的差异不一样,它们的间距也是相等的?您希望上面的示例图显示一条直线吗?不,不是直线不,只是点。曲线不是连续的。就像这样:很好,这简化了事情
plt.plot(range(len(idx)), new_y, 'o')
# ---^--- instead of 'o-'