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
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()
Out: 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
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()))
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 labels
labels: list of label names
rotation: amount of rotation in degrees
horizontalalignment: how the labels should be aligned, options include
fontsize: size of font, can be a string or a number (as we'll see below)
BONUS: use generative AI to customize charts
As important as it is to be able to recognize the code and have a working knowledge of how to customize charts and graphs effectively, generative AI can greatly speed up the process and save you tons of time. In Einblick, we've implemented an AI agent, Einblick Prompt, into each of our AI-native data notebooks. Prompt can create data workflows from as little as one sentence. Check out how we created a comparable graph just using natural language prompts:
Using Generative AI in Einblick
- Open an Einblick canvas (you can fork any embedded canvas!)
- Connect to your data > Create a Table
- Select the Table > Click the Prompt icon
- Type in: "Create a countplot of art museums, history museums, arboretums, and science museums."
- Select the Python cell > Shift + Enter to run code
- Select the Python cell > Click the Prompt icon
- Type in: "Duplicate the plot but make sure the axis tick labels don't overlap"
Let us know if you try out Prompt, and share any results you get!
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()
In this example, we used a number (
fontsize = 8) for font size. Additionally, we created a list of labels called
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()
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:
label: string, actual label
fontsize: size of the font, can be number or string
fontfamily: name of the font or a generic font family name like "serif" or "monospace" (to see the fonts available, you can use
fontweight: how the label should be weighted, such as "bold" or "semi-bold"
horizontalalignment: alignment of the label
Einblick is an AI-native data science platform that provides data teams with an agile 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 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.