#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('run', '../../common_functions/import_all.py') from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from common_functions.setup_notebook import set_css_style, setup_matplotlib, config_ipython config_ipython() setup_matplotlib() set_css_style() # # Some simple notes on Matplotlib functionalities # ## A line plot # In[2]: x = np.arange(0, 10, 0.1) y = np.sin(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('sin(x)') plt.title('Sine function', fontweight='bold', fontsize=16) plt.show(); #plt.savefig('sine.png') # to save # type of markers, line, can be set as kwargs in plot() # ## a 3D plot # In[3]: x = np.array([i for i in range(-100, 100)]) y = np.array([i for i in range(-100, 100)]) x, y = np.meshgrid(x, y) def f(x, y): return x**2 + y**2 fig = plt.figure() ax = fig.gca(projection='3d') parabola = ax.plot_surface(x, y, f(x, y), cmap=cm.RdPu) plt.xlabel('x') plt.ylabel('y') plt.show(); # ## A bar plot # In[4]: # Some dummy data data = {'a': 10, 'b': 20, 'c': 15, 'd': 25} plt.bar([i for i in range(len(data.keys()))], data.values()) # does not read str xtics directly, have to set xticks plt.xticks([i for i in range(len(data.keys()))], data.keys()) plt.show(); # there are also other types, like scatter plot or hist # ## Using log scales # In[5]: # Exp in semilog x = np.linspace(0, 1, 100) #plt.semilogy(x, np.exp(x)) # pow law in log-log x = np.linspace(0, 1, 100) plt.loglog(x, x**(-0.6)) plt.show(); # ## Setting a customised legend # In[6]: x = np.linspace(0, 10) sin_line, = plt.plot(x, np.sin(x), label='sin(x)') cos_line, = plt.plot(x, np.cos(x), label='cos(x)') #plt.legend(handler_map={sin_line: HandlerLine2D(numpoints=2)}, loc=4) # with the Handler plt.legend(loc=4) plt.title('Sin and cos', fontweight='bold', fontsize=16) plt.xlabel('x') plt.show(); # ## Plotting errorbars # In[7]: plt.errorbar([i for i in range(10)], [i for i in range(10)], yerr=[i for i in range(10)], label='avg') plt.show(); # ## Amesome: can use [xkcd](https://xkcd.com)'s style! # # In order to make this work best, the proper font of xkcd, Humor Sans has to be downloaded on the system. You can find it [here](https://github.com/shreyankg/xkcd-desktop/blob/master/Humor-Sans.ttf) (on a Mac, just double click on the downloaded file and click install). # # Then, you should clear the font cache of Matplotlib otherwise it does not pick up newly installed fonts: # # ``` # rm ~/.matplotlib/fontList.cache # ``` # # You may also need to clear the cached font_manager Matplotlib instance, so better run # # ``` # rm ~/.matplotlib/fontList.py3k.cache # ``` # # This will clear our everything and make sure Matplotlib rebuilds its font cache the next time it is imported. # In[8]: matplotlib.rcParams.update({"text.usetex": False}) plt.xkcd() plt.plot([i for i in range(10)], np.sin([i for i in range(10)])) plt.title('A sine wave') plt.xlabel('x') plt.ylabel('sin(x)') plt.show(); # ## References # # 1. [A great article on xkcd arriving to Matplotlib](https://jakevdp.github.io/blog/2013/07/10/XKCD-plots-in-matplotlib/) on Pythonic Perambulations, by Jake VanderPlas (great blog btw!) # 1. [Matplotlib's own showcase of the xkcd style](http://matplotlib.org/xkcd/examples/showcase/xkcd.html) # In[ ]: