Matplotlib is arguably the most popular visualization library in Python. The library is big and it is hard to really learn everything in the library. I have shared quite a lot of plots, tricks, and tips to make nicer visuals in matplotlib. In this article, I want to share some simple but effective techniques to make the plots look nicer and more attractive.
Dark Background
The first one is just a simple dark background. You can make a simple line plot make nicer looking by simply a dark background. Here it is:
import matplotlib.pyplot as plt
from matplotlib import style
with plt.style.context('dark_background'):
plt.plot([1, 12, 4, 10, 3, 11, 2], 'r-o',)
plt.plot([3, 9, 2, 7, 6, 14, 4], 'g-v')
plt.show()

Isn’t it nicer than a regular default style?
Cyberpunk
There is another dark background option with some more styles that can be installed using this:
!pip install mplcyberpunk
Let’s use this style on the same plot:
import mplcyberpunk
plt.style.use("cyberpunk") plt.figure(figsize=(8, 5)) plt.plot([1, 12, 4, 10, 3, 11, 2], marker = 'v') plt.plot([3, 9, 2, 7, 6, 14, 4], marker = 'd') plt.plot([2, 5, 9, 15, 7, 10, 6], marker = 's') mplcyberpunk.add_glow_effects() plt.show()

Notice, that we used a glow effect as well to enhance the look a little further.
Broken Bars
Keeping this same style, the next plot will be the broken bar plot. A regular bar plot does not work very well when the range of data is extreme. In a dataset, if you have an extreme range in the data broken bar plot can be an option to visualize them.
plt.figure(figsize = (8, 5)) xranges = [(3, 4), (100, 10), (200, 20)] yrange = (50, 7) plt.broken_barh(xranges, yrange, facecolors='green') xranges = [(8,3), (170, 19),(240, 21)] yrange = (45, -6) plt.broken_barh(xranges, yrange, facecolors='blue') xranges = [(13, 6), (210, 50),(400, 100)] yrange = (30, 2) plt.broken_barh(xranges, yrange, facecolors='red') plt.xlabel('Sales') plt.ylabel('Days of the Month') plt.show()

This time I did not use any glow effect.
Table to the Plot
For the next plots, we will use the heart dataset from Kaggle. Please feel free to download the dataset from this link:
Here are the top five rows of the data:
import pandas as pd
df = pd.read_csv("Heart.csv")
df.head()

Look at the chest pain column. There are four types of chest pain categories. Let’s make a DataFrame where the chest pain categories will be the columns and the corresponding RestBPs will be the row values:
cp = df.set_index([df.groupby('ChestPain').cumcount(), 'ChestPain'])['RestBP'].unstack()
cp = cp.dropna()
cp

The next plot will plot the mean RestBP for each type of chest pain and below each point, the corresponding data will show.
plt.plot(cp_array.mean(axis = 0), '-o', label = "Mean chestpain", c='red') plt.xticks([]) plt.table(cellText=[['%1.2f' % xxx for xxx in xx] for xx in cp_array], cellColours = plt.cm.GnBu(cp_array), loc = "bottom") plt.show()

This may not be helpful for a too big dataset. But for a not-so-big dataset, it is very helpful.
Figure Image
Sometimes adding a picture can make a simple plot more presentable. In the above plot, we were working on chest pain data. See how the plot looks when we add a chest x-ray image to the plot:
import matplotlib.image as image im = image.imread("xray.jpg") plt.figure(figsize=(6, 6)) plt.plot(cp_array.mean(axis = 0), '-o', label = "Mean chestpain", c = 'red') plt.figimage(im, 60, 29, cmap = "ocean", alpha=0.2)

Please feel free to download the x-ray image I used from this link.
Please try it with a different picture in the background. You may find more interesting pictures for this.
XKCD
Lastly, I want to introduce another fun figure that will introduce a cartoonish look to it. You may find a suitable audience for this type of plot too.
plt.style.use('default')
plt.figure(figsize=(10, 6))
plt.xkcd(scale=3, length=400)
sns.boxplot(x = "ChestPain", y = "RestBP",data = df)

Please feel free to change the value of the parameters in the plt.xkcd() function and play with them.
Conclusion
There is a lot of fun element in the Matplotlib library. I tried to introduce some interesting and fun elements of Matplotlib plots. I hope you will be able to use them in your own projects.
Feel free to follow me on Twitter and like my Facebook page.
#DataAnalysis #DataVisualization #python #matplotlib #DataScience