Matplotlib 扩大X轴标签之间的间距

2024-03-29 11:30:50 发布

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

  1. 我想在x轴上增加标签之间的间距,这样它们就不会并排在一起了。

  2. 不管怎样,我可以水平地“拖动”绘图,就像我在excel中可以做的那样,当我水平地向右拖动绘图时,整个绘图会变大。

以下是当前截图: enter image description here

我确实用了代码ax.xaxis.get_儿童()1。设置大小(100),但它不起作用。在

               import matplotlib.pyplot as plt
               import numpy as np
               line = plt.figure()    
               plt.plot(x,y, 'r-',marker='o', color='b')
               plt.grid(True)
               plt.xticks(x, Quickdatres,rotation="vertical")

               ax=plt.subplot()
               ax.xaxis.get_children()[1].set_size(100)
               for label in ax.xaxis.get_ticklabels()[::2]:  
               label.set_visible(False)   

               plt.show()

Quickdatres包含x轴的所有标签。谢谢!在


Tags: import绘图getas水平plt标签ax
1条回答
网友
1楼 · 发布于 2024-03-29 11:30:50

您可以将标签之间的间隔从3增加到5。(我必须构造假数据才能绘制)。在

import matplotlib.pyplot as plt
import numpy as np


x = np.arange(402, 630)
Quickdatres = [str(_) if _%5==0 else '' for _ in x]
y = np.random.randint(500, 3000, 630-402)

line = plt.figure()    
plt.plot(x, y, 'r-', marker='o', color='b')
plt.grid(True)
plt.xticks(x, Quickdatres, rotation="vertical")

ax = plt.subplot()
ax.xaxis.get_children()[1].set_size(100)
for label in ax.xaxis.get_ticklabels()[::2]:  
    label.set_visible(False)   

plt.show()

enter image description here

相关问题 更多 >