optional xkcd plot style
This commit is contained in:
		
							parent
							
								
									b5607deecb
								
							
						
					
					
						commit
						6604261978
					
				
							
								
								
									
										36
									
								
								plotstyle.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								plotstyle.py
									
									
									
									
									
								
							| @ -1,6 +1,8 @@ | |||||||
| import matplotlib as mpl | import matplotlib as mpl | ||||||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||||||
| 
 | 
 | ||||||
|  | xkcd_style = False | ||||||
|  | 
 | ||||||
| # colors: | # colors: | ||||||
| colors = { | colors = { | ||||||
|     'red': '#CC0000', |     'red': '#CC0000', | ||||||
| @ -99,8 +101,38 @@ def show_spines(ax, spines): | |||||||
|             ax.yaxis.set_ticks_position('both') |             ax.yaxis.set_ticks_position('both') | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def set_xlabel(ax, label, unit=None): | ||||||
|  |     if not unit: | ||||||
|  |         ax.set_xlabel(label) | ||||||
|  |     elif xkcd_style: | ||||||
|  |         ax.set_xlabel('%s / %s' % (label, unit)) | ||||||
|  |     else: | ||||||
|  |         ax.set_xlabel('%s [%s]' % (label, unit)) | ||||||
|  | 
 | ||||||
|  |          | ||||||
|  | def set_ylabel(ax, label, unit=None): | ||||||
|  |     if not unit: | ||||||
|  |         ax.set_ylabel(label) | ||||||
|  |     elif xkcd_style: | ||||||
|  |         ax.set_ylabel('%s / %s' % (label, unit)) | ||||||
|  |     else: | ||||||
|  |         ax.set_ylabel('%s [%s]' % (label, unit)) | ||||||
|  | 
 | ||||||
|  |              | ||||||
| # initialization: | # initialization: | ||||||
| plt.xkcd() | if xkcd_style: | ||||||
|  |     plt.xkcd() | ||||||
|  |     bar_fac = 0.9 | ||||||
|  |     mpl.rcParams['xtick.major.size'] = 6 | ||||||
|  |     mpl.rcParams['ytick.major.size'] = 6 | ||||||
|  | else: | ||||||
|  |     bar_fac = 1.0 | ||||||
|  |     mpl.rcParams['font.family'] = 'sans-serif' | ||||||
|  |     mpl.rcParams['xtick.labelsize'] = 'small' | ||||||
|  |     mpl.rcParams['ytick.labelsize'] = 'small' | ||||||
|  |     mpl.rcParams['xtick.major.size'] = 2.5 | ||||||
|  |     mpl.rcParams['ytick.major.size'] = 2.5 | ||||||
|  |     mpl.rcParams['legend.fontsize'] = 'x-small' | ||||||
| mpl.rcParams['figure.facecolor'] = 'white' | mpl.rcParams['figure.facecolor'] = 'white' | ||||||
| #mpl.rcParams['axes.spines.left'] = True    # newer matplotlib only | #mpl.rcParams['axes.spines.left'] = True    # newer matplotlib only | ||||||
| #mpl.rcParams['axes.spines.bottom'] = True | #mpl.rcParams['axes.spines.bottom'] = True | ||||||
| @ -108,8 +140,6 @@ mpl.rcParams['figure.facecolor'] = 'white' | |||||||
| #mpl.rcParams['axes.spines.right'] = False | #mpl.rcParams['axes.spines.right'] = False | ||||||
| mpl.rcParams['xtick.direction'] = 'out' | mpl.rcParams['xtick.direction'] = 'out' | ||||||
| mpl.rcParams['ytick.direction'] = 'out' | mpl.rcParams['ytick.direction'] = 'out' | ||||||
| mpl.rcParams['xtick.major.size'] = 6 |  | ||||||
| mpl.rcParams['ytick.major.size'] = 6 |  | ||||||
| mpl.rcParams['xtick.major.width'] = 1.25 | mpl.rcParams['xtick.major.width'] = 1.25 | ||||||
| mpl.rcParams['ytick.major.width'] = 1.25 | mpl.rcParams['ytick.major.width'] = 1.25 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ import numpy as np | |||||||
| import scipy.stats as st | import scipy.stats as st | ||||||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||||||
| import matplotlib.gridspec as gridspec | import matplotlib.gridspec as gridspec | ||||||
| from plotstyle import colors, cm_size, show_spines | from plotstyle import colors, cm_size, show_spines, set_xlabel, set_ylabel, bar_fac | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     # wikipedia: |     # wikipedia: | ||||||
| @ -21,8 +21,8 @@ if __name__ == "__main__": | |||||||
|     ax1 = fig.add_subplot(spec[0, 0]) |     ax1 = fig.add_subplot(spec[0, 0]) | ||||||
|     show_spines(ax1, 'lb') |     show_spines(ax1, 'lb') | ||||||
|     ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50) |     ax1.scatter(indices, data, c=colors['blue'], edgecolor='white', s=50) | ||||||
|     ax1.set_xlabel('index') |     set_xlabel(ax1, 'index') | ||||||
|     ax1.set_ylabel('Weight / kg') |     set_ylabel(ax1, 'Weight', 'kg') | ||||||
|     ax1.set_xlim(-10, 310) |     ax1.set_xlim(-10, 310) | ||||||
|     ax1.set_ylim(0, 370) |     ax1.set_ylim(0, 370) | ||||||
|     ax1.set_yticks(np.arange(0, 351, 100)) |     ax1.set_yticks(np.arange(0, 351, 100)) | ||||||
| @ -31,11 +31,11 @@ if __name__ == "__main__": | |||||||
|     show_spines(ax2, 'lb') |     show_spines(ax2, 'lb') | ||||||
|     xx = np.arange(0.0, 350.0, 0.5) |     xx = np.arange(0.0, 350.0, 0.5) | ||||||
|     yy = st.norm.pdf(xx, mu, sigma) |     yy = st.norm.pdf(xx, mu, sigma) | ||||||
|     ax2.plot(yy, xx, color=colors['red']) |     ax2.plot(yy, xx, color=colors['red'], lw=2) | ||||||
|     bw = 20.0 |     bw = 20.0 | ||||||
|     h, b = np.histogram(data, np.arange(0, 401, bw)) |     h, b = np.histogram(data, np.arange(0, 401, bw)) | ||||||
|     ax2.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], height=0.9*bw, align='edge') |     ax2.barh(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], height=bar_fac*bw, align='edge') | ||||||
|     ax2.set_xlabel('pdf / 1/kg') |     set_xlabel(ax2, 'pdf', '1/kg') | ||||||
|     ax2.set_xlim(0, 0.012) |     ax2.set_xlim(0, 0.012) | ||||||
|     ax2.set_xticks([0, 0.005, 0.01]) |     ax2.set_xticks([0, 0.005, 0.01]) | ||||||
|     ax2.set_xticklabels(['0', '0.005', '0.01']) |     ax2.set_xticklabels(['0', '0.005', '0.01']) | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ import numpy as np | |||||||
| import scipy.stats as st | import scipy.stats as st | ||||||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||||||
| import matplotlib.gridspec as gridspec | import matplotlib.gridspec as gridspec | ||||||
| from plotstyle import colors, cm_size, show_spines | from plotstyle import colors, cm_size, show_spines, set_xlabel, set_ylabel, bar_fac | ||||||
| 
 | 
 | ||||||
| def boltzmann(x, x0, k): | def boltzmann(x, x0, k): | ||||||
|     return 1.0/(1.0+np.exp(-k*(x-x0))) |     return 1.0/(1.0+np.exp(-k*(x-x0))) | ||||||
| @ -27,8 +27,8 @@ if __name__ == "__main__": | |||||||
|     show_spines(ax1, 'lb') |     show_spines(ax1, 'lb') | ||||||
|     ax1.plot(xx, yy, colors['red'], lw=2) |     ax1.plot(xx, yy, colors['red'], lw=2) | ||||||
|     ax1.scatter(x, y, c=colors['blue'], edgecolor='white', s=50) |     ax1.scatter(x, y, c=colors['blue'], edgecolor='white', s=50) | ||||||
|     ax1.set_xlabel('Hair deflection / nm') |     set_xlabel(ax1, 'Hair deflection', 'nm') | ||||||
|     ax1.set_ylabel('Open probability') |     set_ylabel(ax1, 'Open probability') | ||||||
|     ax1.set_xlim(-20, 20) |     ax1.set_xlim(-20, 20) | ||||||
|     ax1.set_ylim(-0.2, 1.17) |     ax1.set_ylim(-0.2, 1.17) | ||||||
|     ax1.set_xticks(np.arange(-20.0, 21.0, 10.0)) |     ax1.set_xticks(np.arange(-20.0, 21.0, 10.0)) | ||||||
| @ -41,11 +41,11 @@ if __name__ == "__main__": | |||||||
|     ax2.plot(xg, yg, colors['red'], lw=2) |     ax2.plot(xg, yg, colors['red'], lw=2) | ||||||
|     bw = 0.05 |     bw = 0.05 | ||||||
|     h, b = np.histogram(y-boltzmann(x, x0, k), np.arange(-1.0, 1.01, bw)) |     h, b = np.histogram(y-boltzmann(x, x0, k), np.arange(-1.0, 1.01, bw)) | ||||||
|     ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=0.9*bw, align='edge') |     ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=bar_fac*bw, align='edge') | ||||||
|     ax2.set_xlabel('residuals') |     set_xlabel(ax2, 'residuals', 'nm') | ||||||
|     ax2.set_ylabel('pdf') |     set_ylabel(ax2, 'pdf') | ||||||
|     ax2.set_xlim(-0.3, 0.3) |     ax2.set_xlim(-0.3, 0.3) | ||||||
|     #ax2.set_ylim(0, 370) |     ax2.set_ylim(0, 5.05) | ||||||
|     #ax2.set_xticks([0, 0.005, 0.01]) |     #ax2.set_xticks([0, 0.005, 0.01]) | ||||||
|     #ax2.set_xticklabels(['0', '0.005', '0.01']) |     #ax2.set_xticklabels(['0', '0.005', '0.01']) | ||||||
|     #ax2.set_yticks(np.arange(0, 351, 100)) |     #ax2.set_yticks(np.arange(0, 351, 100)) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user