Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - Matplotlib increase spacing between points on x-axis

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.

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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=__)

as seen in the code below:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...