Matthew Ogbuehi
Matthew Ogbuehi

Reputation: 11

How to change the title size of a plot in pandas (matplotlib)?

I read the documentation and even the github link to the source code and I don't see a kwarg to pass in for title size, only for the x and y axis labels. The code below increase size of everything in the figure besides the title. How do people usually increase the title size as well? Thanks!

import pandas as pd
import pathlib as Path

a_path = Path('../'data.csv')
a_dataframe = pd.read_csv(a_path)
a_dataframe.plot(title='Some Title', figsize=(50,25), fontsize=40)

Upvotes: 1

Views: 3435

Answers (2)

elena.kim
elena.kim

Reputation: 958

If you want to use matplotlib.pyplot, try below codes.

Sample1

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

fig = plt.figure(figsize=(12, 4))
ax = fig.add_subplot(121)
ax.plot(x, y, color='lightblue', linewidth=3)
plt.title("Title", fontsize=20)

ax2 = fig.add_subplot(122)
ax2.plot(x, y, color='lightblue', linewidth=3)
plt.title("Title", fontsize=30)

Sample2

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, color='lightblue', linewidth=3)
ax.set_title('Title', fontdict={'fontsize': 15, 'fontweight': 'bold'})

Upvotes: -1

tdy
tdy

Reputation: 41327

You can save the axes handle and then call .title.set_size():

ax = a_dataframe.plot(title='Some Title', figsize=(50,25), fontsize=40)
ax.title.set_size(40)

Toy example:

df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,0]})
ax = df.plot(title='ax.title.set_size(30)')
ax.title.set_size(30)

figure using ax.title.set_size()

Upvotes: 2

Related Questions