Did you wonder how to add to graphs a cartoon-like touch? Well, one option is to use matplotlib evoking xkcd function in python. Still, this small code snippet provides a useful information how to customize a plot (adding annotations, coloring, removing axis) using matplotlib.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# -*- coding: utf-8 -*- """ Created on Wed Nov 2 09:32:52 2016 @author: Krzysztof """ #import libraries import numpy as np import matplotlib.pyplot as plt #to have a comics like style plt.xkcd() #generate a 10000 points between 0 and 200 X = np.linspace(0,200,10000) #generate the y values - an equation for Damped Harmonic Oscillator Y = np.cos(0.5*X)*np.exp(-.03*X) #make an figure object wiht axes fig = plt.figure(figsize=(12,5), dpi=300,facecolor="white") axes = plt.subplot(111) #plot data with red color with opacity and solid line zorder plt.plot(X,Y, color = 'red', linewidth=2, linestyle="-", zorder=+5,alpha=0.5) #this resets x and y ticks axes.set_xticks([]) axes.set_yticks([]) #commands to set limits for x and y axis excess=1.15 axes.set_xlim( excess*X.min(), excess*X.max() ) axes.set_ylim( excess*Y.min(), excess*Y.max() ) #add a point (as a scatter plot) and annotate it t = [9900] plt.scatter( X[t], Y[t], s=50, zorder=+12, c='black') plt.text(X[t[0]]-15, Y[t[0]]+.05, "I'm done here!", ha='left', va='bottom') #add y label plt.ylabel("LEFT 0 RIGHT",y=.5, fontsize=20) #add the text for x axis plt.text(X[100], Y.min()-0.15, "Time this way->",ha='left', va='top', color='black', fontsize=20) #add color for positive and negative values of Y axes.fill_between(X*2, 0, Y.max()*10, color = '0.85', zorder=-1) axes.fill_between(X*2, 0, Y.min()*10, color = (1.0,1.0,0.9), zorder=-1) #add vertical liness axes.axvline(X[400],ymin=0,ymax=Y.max()*excess, color='.5', ls='--') axes.axvline(X[3000],ymin=0,ymax=Y.max()*excess, color='.5', ls='--') #annotate the meaning between vertical lines and put arrow plt.text(X[1400], 0.95, "WOOHOO",ha='left', va='top', color='black', fontsize=12) plt.text(X[900], 0.8, "craziness zone",ha='left', va='top', color='black', fontsize=12) plt.annotate("", xytext=(X[400],0.85), xy=(X[3000], 0.85), arrowprops=dict(arrowstyle="<->")) #add a horizontal line at 0 plt.axhline(color='gray',linewidth=1,zorder=2) #add title plt.title("Life of a pendulum",fontsize=24) #save a png plt.savefig("pendulum.png") plt.show() |