We've all worked through hours of analysis, building models, cleaning data, and then the deadline to share results comes up. Now it's about how to present data, findings, early exploratory data analysis, to share the compelling data story and logic behind each step of the process. But sometimes plots start out messy, like the one below.

In this post, we'll use matplotlib
and seaborn
together to create customized, beautiful axis labels, axis tick labels, and titles for your plots so that your data can speak for itself. Click through our table of contents on the left to see each example, or open and fork the canvas below for the full code.
For context, the dataset comes from Kaggle, and contains information about museums around the United States.
Import and setup
Note: even though we're using seaborn
to create the base visualizations, we need matplotlib
to customize the various axis and axis tick labels.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()
df["Museum Type"].value_counts()
Output:
Out[5]:
HISTORY MUSEUM 1093
ART MUSEUM 380
ARBORETUM, BOTANICAL GARDEN, OR NATURE CENTER 292
SCIENCE & TECHNOLOGY MUSEUM OR PLANETARIUM 132
Name: Museum Type, dtype: Int64
From the output, we can see that the subset of the data has only 4 museum types. Now let's create a count plot, using seaborn
.
Example 1: rotate axis tick labels
# Customize x-axis tick labels version 1
plot = sns.countplot(data = df, x = "Museum Type")
# Get x-axis tick locations and labels
xtick_loc = plot.get_xticks()
xtick_labels = plot.get_xticklabels()
# Set the x-axis ticks
plot.set_xticks(ticks = xtick_loc, labels = xtick_labels, rotation = 45, ha = 'right', fontsize = 'x-small')
print("x-axis tick locations: " + str(xtick_loc))
print("x-axis tick labels: " + str(plot.get_xticklabels()))
Output:
x-axis tick locations: [0 1 2 3]
x-axis tick labels: [Text(0, 0, 'ART MUSEUM'), Text(1, 0, 'HISTORY MUSEUM'), Text(2, 0, 'ARBORETUM, BOTANICAL GARDEN, OR NATURE CENTER'), Text(3, 0, 'SCIENCE & TECHNOLOGY MUSEUM OR PLANETARIUM')]

From the above code, we can see that we can get the location of the x-axis ticks using plot.get_xticks()
and we can get the labels that the plot will use via plot.get_xtickslabels()
. Then we feed the following arguments into the function plot.set_xticks(ticks, labels, rotation, ha, fontsize)
:
ticks
: location of the tick labelslabels
: list of label namesrotation
: amount of rotation in degreesha
orhorizontalalignment
: how the labels should be aligned, options include"center"
,"left"
,"right"
fontsize
: size of font, can be a string or a number (as we'll see below)
Example 2: more axis tick label customization – alignment, size, font family, etc.
# Create list of labels that wrap the longer labels
xtick_labels = ["ART MUSEUM", "HISTORY MUSEUM", "ABORETUM, BOTANICAL GARDEN, \n OR NATURE CENTER",
"SCIENCE & TECHNOLOGY MUSEUM \n OR PLANETARIUM"]
# Plot data
plot = sns.countplot(data = df, x = "Museum Type")
# Customize x-axis tick labels
plot.set_xticks(ticks = xtick_loc, labels = xtick_labels, rotation = 45, ha = 'right', fontsize = 8)
# Specfiy axis labels and title
plot.set(xlabel = 'Musem Type', ylabel = 'Count', title = 'Number of Museums by Type')
plt.show()
Output:

In this example, we used a number (fontsize = 8
) for font size. Additionally, we created a list of labels called xtick_labels
(labels = xtick_labels
) to help with the formatting. In addition, we used the plot.set()
function to add in and customize the x-axis and y-axis labels, as well as the title. Note that this function only lets you edit the text, NOT the appearance of the text. We'll do that in the next example.
Example 3: customize axis labels and title
# Create plot
plot = sns.countplot(data = df, x = "Museum Type")
# Customize x-axis tick labels
plot.set_xticks(ticks = xtick_loc, labels = xtick_labels, rotation = 45, ha = 'right', fontsize = 8)
# Specfiy axis labels and title
plot.set_xlabel("Customized X-axis label", fontsize = 18, fontfamily = "DejaVu Sans Mono")
plot.set_ylabel("Customized Y-axis label", fontsize = 10, fontweight = "bold")
plot.set_title("Customized Title", fontsize = 24, fontfamily = "serif", horizontalalignment = "center")
plt.show()
Output:

In this final example, we use a few plot.set_XXX()
functions to customize the appearance of the x-axis label (set_xlabel
), y-axis label (set_ylabel
), and the title (set_title
). We customized them using the following arguments:
xlabel
,ylabel
,label
: string, actual labelfontsize
: size of the font, can be number or stringfontfamily
: name of the font or a generic font family name like "serif" or "monospace" (to see the fonts available, you can usematplotlib's font_manager
)fontweight
: how the label should be weighted, such as "bold" or "semi-bold"horizontalalignment
: alignment of the label
About
Einblick is an agile data science platform that provides data scientists with a collaborative workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick customers include Cisco, DARPA, Fuji, NetApp and USDA. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.