Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Make Your Matplotlib Plots Stand Out Using This Cheat Sheet
Latest

Make Your Matplotlib Plots Stand Out Using This Cheat Sheet

Last Updated on May 24, 2022 by Editorial Team

Author(s): Arslan Shahid

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

Cheat sheet for editing the background, ticks, and annotations in Matplotlib

Image by Hunter Harriet onΒ Unsplash

Matplotlib is the most extensive plotting library in python, arguably one of the most frequently used. If you’re like me and you often forget the precise code to format plots, this piece is written specifically forΒ you.

Background

One of the easiest and simplest ways to make your graphs stand out is to change the default background. I prefer to use β€˜seaborn-whitegrid’, as I think it is simple and minimal and most colors look good on a white background. Here is theΒ code:

import matplotlib.pyplot as plt
#the print statement tells all available style sheets
print(plt.style.available)
#This line of code changes the style sheet
plt.style.use(β€˜seaborn-whitegrid’)
Left- Default style and Right seaborn-whitegridβ€Šβ€”β€Šimage by author. You can view all style sheetsΒ here

Title, Labels &Β ticks

A good infographic needs an appropriate title, labels, and also appropriate tick marks. When dealing with money it is better to use a currency symbol like β€˜$’ and when dealing with large numbers it is preferred that you format them into thousands(K), Millions(M), billions(B), etc. Smaller numbers should be rounded to the nearest decimal, nothing beyond 3 decimal places is ever really needed. The optimal in my opinion is 2 decimal places. You should also multiply frequencies by 100 to make them percentages and also add β€˜,’ after every third digit in a big number i.e 1000000 should be written as 1,000,000.

Lastly, the title & ticks must be visible, so readable size is necessary. Below find the code to do each of theseΒ things:

#importing and creating figure
from matplotlib import ticker
fig,ax = plt.subplots(figsize=(20,20))
#Add title and set font size
plt.title(β€˜Title’,fontsize=50)
#Add xaxis label & yaxis label, with fontsize
plt.xlabel('xlabel',fontsize=30)
plt.ylabel('ylabel',fontsize=30)
#tick-size and weight [ 'normal' | 'bold' | 'heavy' | 'light' | #'ultrabold' | 'ultralight']
plt.yticks(fontsize=20,weight='bold')
plt.xticks(fontsize=20,weight='bold')
#Format ticks as currency or any prefix (replace $ with your choice)
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("${x}"))
#Format ticks as distance or any suffix (replace km with your #choice)
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x} km"))
#Format ticks decimal point, the number preceding f denotes how many
# decimal points e.g use .3f for three
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.2f}"))
#Add , for large numbers e.g 10000 to 10,000
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:,}"))
#Format as a percentage 
ax.xaxis.set_major_formatter(ticker.PercentFormatter())
#Format thousands e.g 10000 to 10.0K
ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: format(x/1000,'1.1f')+'K'))
#Format Millions e.g 1000000 to 1.0M
ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: format(x/1000000,'1.1f')+'M'))
#Example of combined tick mark, currency with , & 2 decimal #precision
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("${x:,.2f}"))

Annotations

Annotations are a great way to highlight any specific value or point. Combining h-lines (horizontal lines)& v-lines (vertical lines) you can indicate axis labels for a particular point. Here are some examples of what you canΒ do.

Bitcoin Price chart with all-time-high highlighted by annotation & v-line. Read more about this here. Image byΒ author
Percentile of the score with current match performance highlighted with annotation, h-line & v-line. read more about this here. Image byΒ author

Here is how you can add an annotation:

#First argument is the text, ha is horizontal alignment, va is #vertical alignment, xy is coordinates of the pojnt xytext are the #coordinates of the text (if you want the text to be away from the #point)
ax.annotate('annotation',ha='center',va='center', xy = (0.5, 0.5), xytext=(0.51,0.51),fontsize=30)
#Add v-line with xcoord, min & max ranges with y and aesthetic #properties
plt.vlines(x=0.5,ymin=-0.05,ymax=0.5,ls=':',lw=3,color='darkblue')
#Add h-line with ycoord, min & max ranges with xand aesthetic #properties
plt.hlines(y=0.5,xmin=-0.05,xmax=0.5,ls=':',lw=3,color='darkblue')
#Optional code for house keeping
plt.xticks(fontsize=25,weight='bold')
plt.yticks(fontsize=25,weight='bold')
plt.xlim(0,1)
plt.ylim(0,1)
plt.title('Example of Annotation',fontsize=35)
Result of the above code, how you can add one point annotation. Image by theΒ author.

Sizing, limits, andΒ legends

Often we need to resize or reshape our graph. It is also prudent to specify limits for the x-axis & y-axis, especially when there are some unusual outliers in the data. Also, it is very helpful to include a legend, with accurate labels and markerΒ sizes.

Here is theΒ code:

#Change figure size by adjusting figsize parameter
fig,ax = plt.subplots(figsize=(25,25))
#Annotation, v-lines & h-lines as explained above
ax.annotate(β€˜Blue’,ha=’center’,va=’center’, xy = (0.5, 0.5), xytext=(0.51,0.51),fontsize=30,color=’darkblue’)
plt.vlines(x=0.5,ymin=-0.05,ymax=0.5,ls=’:’,lw=3,color=’darkblue’)
plt.hlines(y=0.5,xmin=-0.05,xmax=0.5,ls=’:’,lw=3,color=’darkblue’)
#Scatter plot of one point, label parameter goes in legend 
# you can add '_no_legend_' to hide this label in the legend
ax.scatter(x=0.5,y=0.5,label=’Blue point’)
#Annotation, v-lines & h-lines as explained above
ax.annotate(β€˜Red’,ha=’center’,va=’center’, xy = (0.75, 0.75), xytext=(0.76,0.76),fontsize=30,color=’darkred’)
plt.vlines(x=0.75,ymin=-0.05,ymax=0.75,ls=’:’,lw=3,color=’darkred’)
plt.hlines(y=0.75,xmin=-0.05,xmax=0.75,ls=’:’,lw=3,color=’darkred’)
#Scatter plot of one point, label parameter goes in legend
ax.scatter(x=0.75,y=0.75,label=’Red point’)
#House keeping for xticks & yticks explained above
plt.xticks(fontsize=25,weight=’bold’)
plt.yticks(fontsize=25,weight=’bold’)
#xlim specifies xaxis limits , first argument min limit & second #argument max limit
plt.xlim(0,1)
#ylim specifies yaxis limits , first argument min limit & second #argument max limit
plt.ylim(0,1)
#loc parameter tells location explained below
#prop tells legend font properties, read more
here
#markerscale tells how much big should the markers on the legend be #in proportion to actual markers. 2 implies twice as big
plt.legend(loc=1,prop={β€˜size’:25},markerscale=2)
plt.title(β€˜Example of Sizing’,fontsize=35)
specifies loc parameter for the legend, where you can position it. Image by theΒ author.
Result of above code, Image by theΒ author.

I will definitely add more cheats as I learn them. For now, this is it, I hope you found this small tutorialΒ helpful.

Please consider following me! If you’re interested in statistical analysis and good visualizations please check out some of the other articles:

  1. Bitcoin & Ethereumβ€Šβ€”β€ŠFinding statistical relationships in returns: https://medium.datadriveninvestor.com/bitcoin-ethereum-finding-statistical-relationships-in-returns-15cc695f4c1a
  2. Money Balling Cricketβ€Šβ€”β€ŠStatistically evaluating a Match: https://medium.com/mlearning-ai/money-balling-cricket-statistically-evaluating-a-match-9cda986d015e
  3. Lies, Big Lies, and Data Science: https://medium.com/mlearning-ai/lies-big-lies-and-data-science-6147e81fb9fc

Thank you!


Make Your Matplotlib Plots Stand Out Using This Cheat Sheet was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓