Python绘图仅使用x的每第n个元素来标记x轴

标签 python matplotlib plot

抱歉,这个问题可能已被问及回答了数千次,但不知何故我找不到解决方案。

我有两个列表:

x=['text', 'some more text', 'looooong text']
y = [1, 2, 3]

当我用它们绘制时

plt.plot(x,y)

我的 x 列表中的文本不可读,因为它们相互覆盖。

我该怎么做才能在 x 轴上仅显示“文本”和“looooooong 文本”? 可能是 xticks 的东西,但我不明白。

最佳答案

一种方法:

plt.plot(x,y)
plt.xticks(np.arange(3), ['text','','looooong text'])

enter image description here

如果您想保留所有 x 标签而不重叠,请使用:

plt.figure(figsize=(7,5))  # <- increase figure margin
plt.plot(x,y)
plt.tight_layout()   # <- helps to maintain non-overlapping

enter image description here

关于Python绘图仅使用x的每第n个元素来标记x轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644579/

相关文章:

python - 如何在Python中完全复制列表?

python - 如何在 Python 中的图表上显示 R 平方值

python - matplotlib 移动颜色条

python - Bokeh 将图例添加到步骤图

python - 在 Altair 中的绘图上重叠垂直线

python - 在 Python Cloud Function 中使用 errorhandler 返回 JSON

python - Visual Studio Code 中的调用层次结构

Python 脚本策略 : running script twice vs excec

python - 索引中按日期排列的多个箱线图

r - 如何在 R 中的绘图标题中使用两个变量?