python - Matplotlib 增加 x 轴上点之间的间距

标签 python matplotlib

我正在寻找一种方法来防止标签重叠。在通过 Stackoverflow 进行搜索时,我什至找不到关于如何控制 x 轴间距的任何建议。

enter image description here

        matplotlib.pyplot.xticks(x, xticks, rotation=90)
        matplotlib.pyplot.plot(x, y)
        matplotlib.pyplot.bar(x, y, alpha=0.2)

        matplotlib.pyplot.title(
            f"średnia cena produktu {self.identifier}, według kontrahentów")
        matplotlib.pyplot.xlabel("kontrahent")
        matplotlib.pyplot.ylabel("cena")


        matplotlib.pyplot.tight_layout()

        matplotlib.pyplot.savefig(os.path.join(
            "products", self.identifier, "wykres.png"))
        matplotlib.pyplot.close()

最佳答案

首先,如果没有你的数据,很难确切地知道发生了什么,所以我不得不创建虚拟数据并根据你的变量“self.identifier”和“xticks”进行调整,因为我们不知道它们是什么.

话虽这么说,你正在寻找的功能是

plt.tick_params(axis='x', which='major', labelsize=__)

如以下代码所示:

import numpy as np
import matplotlib.pyplot as plt

#make dummy data
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]
y=np.random.rand(len(x))

plt.figure()
plt.plot(x, y)
plt.bar(x, y, alpha=0.2)
plt.title(f"średnia cena produktu, według kontrahentów")
plt.xlabel("kontrahent")
plt.ylabel("cena")
plt.xticks(x, [str(i) for i in y], rotation=90)

#set parameters for tick labels
plt.tick_params(axis='x', which='major', labelsize=3)

plt.tight_layout()

关于python - Matplotlib 增加 x 轴上点之间的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52523710/

相关文章:

python - Tornado :get_argument——我应该自己转义输入吗?

python - 颜色条中的结束刻度 - matplotlib

python - Pandas 值的概率

python - 在 Django 中接受电子邮件地址作为用户名

python - 如何在 Jupyter Notebooks/Lab 中为单个单元格抑制内联 matplotlib?

python-3.x - 如何识别七段显示器上的数字?

python - Matplotlib Python

python - Matplotlib:在刻度之间移动刻度标签

python - CherryPy 用于网络托管控制面板应用程序

python - "KeyError: ' 在 AWS S3 中记录 '"- Lambda 触发器