0

I want to adjust the font size of the x-Axis labels and y-labels.

    plt.style.use("dark_background")
    self.figure = plt.figure(figsize=(12, 2), dpi=120)
    self.canvas = FigureCanvas(self.figure)
    self.figure.clear()

    fig = self.figure.add_subplot(111)
    df = pd.DataFrame({'Year': years, 'Buyer Count': buyerCount})

    for i in df.index:
        word = df.loc[i, "Buyer Count"]
        y = df.loc[i, "Buyer Count"]
        plt.annotate(f'{word:,.0f}', (i, y), ha="center", va="bottom", fontsize=8)

    sns.barplot(x='Year', y='Buyer Count', data=df).set(title="Buyers".format(year))
    self.customers_chart.addWidget(self.canvas)
    self.canvas.show()

X-Label Bar Plot in Question

I have tried using

p = sns.barplot(x='Year', y='Buyer Count', data=df).set(title="Buyers".format(year))
p.set_xlabel("X-Axis", fontsize = 12)
p.set_ylabel("Y-Axis", fontsize = 12)

But an error arises:

AttributeError: 'list' object has no attribute 'set_xlabel'
lloydyu24
  • 753
  • 1
  • 8
  • 17
  • 1
    Do you mean the axis labels, or the tick labels? – DavidG May 26 '22 at 07:53
  • I realized that I am referring to the tick labels. I was confused what they were called. Been searching for 2 days now. – lloydyu24 May 27 '22 at 03:21
  • @DavidG Found the answer plus the list error answer by Z-Y.L below. Example: p.set_xticklabels(years, fontsize=4) to adjust the font size of the years overlapping. Thanks for pointing it out. – lloydyu24 May 27 '22 at 03:41

1 Answers1

1

sns.barplot(x='Year', y='Buyer Count', data=df) returns a matplotlib Axes, while sns.barplot(x='Year', y='Buyer Count', data=df).set(title="Buyers".format(year)) returns a list as the error told.

p = sns.barplot(x='Year', y='Buyer Count', data=df)
p.set(title="Buyers".format(year))
p.set_xlabel("X-Axis", fontsize = 12)
p.set_ylabel("Y-Axis", fontsize = 12)

Don't call .set method directly, and refactor the code as above. This should not give any errors.

Z-Y.L
  • 1,740
  • 1
  • 11
  • 15
  • Okay! This is what I needed. I was confused what they were called. Apparently, I was referred to the "tick labels". I'll search how to adjust the tick labels from here. So, the .set() was one of the problems I didn't see. Thank you very much! – lloydyu24 May 27 '22 at 03:24
  • Found the answer plus the list error answer by @Z-Y.L. Example: p.set_xticklabels(years, fontsize=4) # to adjust the font size of the years overlapping. – lloydyu24 May 27 '22 at 03:40