GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Titles and axis labels in pandas

Search for a graph

HOME PANDAS TITLE

Titles and axis labels in pandas

The plot method of pandas provides arguments to add a title and axis labels directly. For more control over their appearance use the matplotlib methods of the axes returned by plot.

Adding a title

Pass the text of the title to the title argument.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Madrid": [6, 8, 11, 13, 17, 23, 26, 25, 21, 15, 10, 7],
                   "London": [5, 6, 8, 10, 13, 16, 18, 18, 15, 12, 8, 6],
                   "Oslo": [-4, -3, 1, 5, 11, 15, 17, 16, 11, 6, 1, -3]},
                  index = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])

df.plot(title = "Average monthly temperature")
# plt.show()

Add a title to a pandas plot

Axis labels

By default pandas uses the name of the index as label of the X axis. Set the labels of both axes with xlabel and ylabel.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Madrid": [6, 8, 11, 13, 17, 23, 26, 25, 21, 15, 10, 7],
                   "London": [5, 6, 8, 10, 13, 16, 18, 18, 15, 12, 8, 6],
                   "Oslo": [-4, -3, 1, 5, 11, 15, 17, 16, 11, 6, 1, -3]},
                  index = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])

df.plot(xlabel = "Month", ylabel = "Temperature (ºC)")
# plt.show()

Add axis labels to a pandas plot

Title for each subplot

When using subplots = True you can pass a list to title, with one title for each subplot.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Madrid": [6, 8, 11, 13, 17, 23, 26, 25, 21, 15, 10, 7],
                   "London": [5, 6, 8, 10, 13, 16, 18, 18, 15, 12, 8, 6],
                   "Oslo": [-4, -3, 1, 5, 11, 15, 17, 16, 11, 6, 1, -3]},
                  index = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])

df.plot(subplots = True, layout = (3, 1), legend = False, sharey = True,
        title = ["Madrid", "London", "Oslo"])
# plt.show()

Add a title to each subplot in pandas

Title style with matplotlib

plot returns a matplotlib axes object. Use set_title, set_xlabel and set_ylabel to customize the font size, the color, the position or the weight of the texts.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Madrid": [6, 8, 11, 13, 17, 23, 26, 25, 21, 15, 10, 7],
                   "London": [5, 6, 8, 10, 13, 16, 18, 18, 15, 12, 8, 6],
                   "Oslo": [-4, -3, 1, 5, 11, 15, 17, 16, 11, 6, 1, -3]},
                  index = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])

ax = df.plot(linewidth = 2)

ax.set_title("Average monthly temperature", fontsize = 14,
             loc = "left", fontweight = "bold", color = "#1D3557")
ax.set_ylabel("Temperature (ºC)", color = "gray")
# plt.show()

Customize the title of a pandas plot with matplotlib

Figure title

To add a general title over several subplots use plt.suptitle.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Madrid": [6, 8, 11, 13, 17, 23, 26, 25, 21, 15, 10, 7],
                   "London": [5, 6, 8, 10, 13, 16, 18, 18, 15, 12, 8, 6],
                   "Oslo": [-4, -3, 1, 5, 11, 15, 17, 16, 11, 6, 1, -3]},
                  index = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])

df.plot(subplots = True, layout = (3, 1), legend = False, sharey = True)
plt.suptitle("Temperature by city")
# plt.show()

Add a figure title to pandas subplots

See the titles in matplotlib tutorial for more options.

R CODER website
Data visualization

Data visualization

A practical introduction

Buy on Amazon
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also

Colors in pandas

Colors in pandas

Legend in pandas

Legend in pandas

Figure size and styles in pandas

Figure size and styles in pandas

Plotting with pandas

Plotting with pandas

Axis customization in pandas

Axis customization in pandas

Subplots in pandas

Subplots in pandas

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.