0

I'm new to data analysis using python and I'm trying to create a very basic visualization of some Premier League football data. One of these visualizations is the number of corners per home team in the 2018/19 Season.

I've managed to plot the graph I'm looking for, but the names on the X-axis are all over each other and, therefore, unreadable, as seen below: As you can see, the names are not properly distributed on the X-axis

import pandas as pd
import seaborn as sb

dataset = pd.read_csv("/Users/lfarias/Downloads/england-premier-league-matches-2018-to-2019-stats.csv")

dataset.columns

cantos = sb.barplot(x = 'home_team_name', y = 'home_team_corner_count', data = dataset)
cantos.tick_params(labelsize=14)
cantos.set_ylabel("Número de escanteios",fontsize=15)
cantos.set_xlabel("",fontsize=1)

Is there any way I can fix this?

P.S.: I've seen another question similar to the one I'm asking, but it ended up not being useful to me.

Thanks in advance.

byln
  • 3
  • 2
  • Hi, it's better if you copy and paste your code in as a code block so people can copy it to run bits, and so people using screen readers can access it. Providing a minimal working example we can run is also helpful. (Remember, we don't have your data.) – baileythegreen Apr 06 '22 at 00:04
  • Sure! I'm sorry, just getting used to Stack Overflow. I'll put my code and data in. – byln Apr 06 '22 at 00:09
  • Of course; it's something everyone has to learn when they start! – baileythegreen Apr 06 '22 at 00:13
  • `cantos.tick_params(labelsize=14, labelrotation=30)` – JohanC Apr 06 '22 at 00:17
  • `matplotlib.pyplot.Axes.tick_params` affects both axes by default, so that should also rotate the y-axis labels. You'd need to specify `axis='x'` to just do what the OP is asking. – baileythegreen Apr 06 '22 at 00:49

1 Answers1

1

You can rotate the x-tick labels; this will produce a nicer-looking plot than if you just widen the bars enough to make the text not overlap.

Just change the rotation value until you find an angle you like. horizontalalignment can also be set to right or left.

cantos.set_xticklabels(cantos.get_xticklabels(), rotation = 45, horizontalalignment = 'center')
baileythegreen
  • 1,126
  • 3
  • 16
  • Seems good! However, I'm getting an error because of that "g"right before the get_xticklabels() parameter: "name g is not defined". I tried importing matplotlib, but no success so far. Am I missing something else? – byln Apr 06 '22 at 00:36
  • oh! I thought I caught all of those; that was the name of my figure; just a sec. – baileythegreen Apr 06 '22 at 00:42
  • So, that was just the 'calling' of the CSV, right? Cool! It worked perfect. Thank you so much! – byln Apr 06 '22 at 00:48
  • I did: `g = sns.()`, where you called it `cantos`. I caught the one at the start of the line, but not in the paramter. – baileythegreen Apr 06 '22 at 00:51