Top 5 tricks to make plots look better.
A big part of being a good data scientist is having the ability to convey your point using relevant and beautiful graphs and visualizations. Visualizations take months of practice to nail down. But there are some low hanging fruits that can help improve the look & feel of your graphs immediately. We will dive below into some of the easy customizations that can be done to improve the way your graphs look.
- Setting the theme of your plots
This is one of the easiest ways of changing the look and feel of your graphs. There are several themes available. An easy way to look at what is available is by this code
import matplotlib.style as style style.available
style.available
The output of which looks like this:
['seaborn-dark',
'seaborn-darkgrid',
'seaborn-ticks',
'fivethirtyeight',
'seaborn-whitegrid',
'classic',
'_classic_test',
'seaborn-talk',
'seaborn-dark-palette',
'seaborn-bright',
'seaborn-pastel',
'grayscale',
'seaborn-notebook',
'ggplot',
'seaborn-colorblind',
'seaborn-muted',
'seaborn',
'seaborn-paper',
'bmh',
'seaborn-white',
'dark_background',
'seaborn-poster',
'seaborn-deep']
You can set the style of your plot (this is a global setting — will apply to all cells in your jupyter notebook) by the following syntax:
style.use('seaborn-poster') #sets the size of the charts
style.use('ggplot')
Here is how the transformation looks:
2. Changing the color of your bars/lines
Changing the color of your bars makes a big difference in visualization. Often times, if you want one bar to stand out, changing its color to a sharper contrast helps make it stand out. Below we will learn simple tricks to change the color of your bars.
First let us have a look @ what colors can be called by name:
import matplotlib
for name, hex in matplotlib.colors.cnames.items():
colorname.append(name)
colorid.append(hex)
and to see them in a list
zippedcolors = list(zip(colorname, colorid))
zippedcolors = sorted(zippedcolors, key=lambda x: x[1])
The list of 954 colors looks like this:
[('black', '#000000'),
('navy', '#000080'),
('darkblue', '#00008B'),
('mediumblue', '#0000CD'),
('blue', '#0000FF'),
('darkgreen', '#006400'),
('green', '#008000'),
('teal', '#008080'),
('darkcyan', '#008B8B'),
('deepskyblue', '#00BFFF'),
('darkturquoise', '#00CED1'),
('mediumspringgreen', '#00FA9A'),
('lime', '#00FF00'),
('springgreen', '#00FF7F'),
('aqua', '#00FFFF'),
('cyan', '#00FFFF'),
('midnightblue', '#191970'),
('dodgerblue', '#1E90FF'),
('lightseagreen', '#20B2AA'),
('forestgreen', '#228B22'),
('seagreen', '#2E8B57'),
('darkslategray', '#2F4F4F'),
('darkslategrey', '#2F4F4F'),
('limegreen', '#32CD32'),
('mediumseagreen', '#3CB371'),
('turquoise', '#40E0D0'),
('royalblue', '#4169E1'),
('steelblue', '#4682B4'),
('darkslateblue', '#483D8B'),
('mediumturquoise', '#48D1CC'),
('indigo', '#4B0082'),
...]
An example where the color can be changed is as follows:
barplot = plt.bar(y_pos, odds_number, color = 'darkgreen', alpha = 0.85)
barplot[0].set_color('darkred')
plt.xlabel('Factors', fontsize = 15, weight = 'bold')
What do the other colors look like?
3. Changing the font family
Matplotlib offers various font families. It is worth exploring the various font family to make sure that the graphs blend in with the presentation font. So, if you are making your presentation in ‘Georgia’, then use font-family as Serif
matplotlib.rcParams['font.family'] = "serif"
To get a list of font families available for matplotlib, you can use the following code:
[f.name for f in matplotlib.font_manager.fontManager.afmlist]
4. Setting the style in seaborn
Seaborn has an excellent setting called style that formats the size of the text, plot and titles according to the style that is set. This helps scale all visible aspects of a graph instead of individually changing each of them.
sns.set_context('poster') #Everything is largersns.set_context('paper') #Everything is smallersns.set_context('talk') #Everything is sized for a presentation
5. Choosing a Palette
Choosing a color palette helps in setting the overal tone of your plots. Do you want your bar plots to be in complete harmony with the theme of your presentation? Do red bar plots not look good with a blue theme and does blue color look too gaudy?
Have no fear, color palettes are here. Color palettes change the entire theme of a plot’s colors. Here is an example:
ax = sns.barplot(y= "Deaths", x = "Causes", data = deaths_pd, palette=("Blues_d"))
sns.set_context("poster")
Here is a list of the available palettes & how they look:
Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, Vega20b_r, Vega20c, Vega20c_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spectral, spectral_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
Hope this helps you improving the quality of your graph. We will dive deeper into making visualizations better with some more examples in the next blog post.