Matplotlib 增加 x 轴上点之间的间距 [英] Matplotlib increase spacing between points on x-axis

查看:79
本文介绍了Matplotlib 增加 x 轴上点之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to prevent labels from overlapping. While searching through Stackoverflow I couldn't even find any suggestion on how to control x-axis spacing.

        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",因为我们不知道这些是什么.

Firstly, it's hard to know exactly what's happening, without your data, so I had to create dummy data and adjust for your variables, 'self.identifier', and 'xticks' given that we don;t know what those are.

话虽如此,您正在寻找的功能是

That being said, the function you're looking for is

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

这篇关于Matplotlib 增加 x 轴上点之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆