paper_plots.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. import matplotlib
  3. import matplotlib as mpl
  4. import matplotlib.pyplot as plt
  5. from cycler import cycler
  6. # set global settings
  7. def pre_paper_plot(change=True):
  8. if not change:
  9. # Reset back to defaults
  10. #mpl.rcParams.update(mpl.rcParamsDefault)
  11. mpl.rcdefaults()
  12. # Apply own default config (as indicated in the matplotlibrc file)
  13. params = mpl.rc_params_from_file(mpl.matplotlib_fname())
  14. mpl.rcParams.update(params)
  15. return
  16. plt.rcParams['text.color'] = '000000'
  17. plt.rcParams['patch.facecolor'] = 'blue'
  18. plt.rcParams['patch.edgecolor'] = 'black'
  19. plt.rcParams['axes.facecolor'] = 'white'
  20. plt.rcParams['axes.edgecolor'] = 'black'
  21. plt.rcParams['axes.grid'] = False
  22. plt.rcParams['axes.labelcolor'] = 'black'
  23. #plt.rcParams['axes.color_cycle'] = '8cd0d3, 7f9f7f, cc9393, 93e0e3, dc8cc3, f0dfaf, dcdccc'
  24. plt.rcParams['axes.prop_cycle'] = cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])
  25. plt.rcParams['xtick.color'] = 'k'
  26. plt.rcParams['xtick.direction'] = 'in'
  27. plt.rcParams['ytick.color'] = 'k'
  28. plt.rcParams['ytick.direction'] = 'in'
  29. plt.rcParams['legend.fancybox'] = False
  30. plt.rcParams['figure.facecolor'] = 'white'
  31. plt.rcParams['figure.edgecolor'] = 'white'
  32. plt.rcParams['text.usetex'] = True
  33. plt.rcParams['figure.figsize'] = (8, 3)
  34. plt.rcParams['font.size'] = 12
  35. plt.rcParams['font.family'] = 'Computer Modern'
  36. plt.rcParams['axes.labelsize'] = 14
  37. plt.rcParams['axes.titlesize'] = 14
  38. plt.rcParams['legend.fontsize'] = 10
  39. plt.rcParams['xtick.labelsize'] = 12
  40. plt.rcParams['ytick.labelsize'] = 12
  41. plt.rcParams['savefig.dpi'] = 300
  42. plt.rcParams['xtick.major.size'] = 3
  43. plt.rcParams['xtick.minor.size'] = 3
  44. plt.rcParams['xtick.major.width'] = 1
  45. plt.rcParams['xtick.minor.width'] = 1
  46. plt.rcParams['ytick.major.size'] = 3
  47. plt.rcParams['ytick.minor.size'] = 3
  48. plt.rcParams['ytick.major.width'] = 1
  49. plt.rcParams['ytick.minor.width'] = 1
  50. plt.rcParams['legend.frameon'] = True
  51. plt.rcParams['legend.edgecolor'] = 'k'
  52. plt.rcParams['legend.loc'] = 'best'
  53. plt.rcParams['axes.linewidth'] = 1
  54. plt.rcParams['legend.handlelength'] = 3
  55. def post_paper_plot(change=True, bw_friendly=False, adjust_spines=False, sci_y=False):
  56. if not change:
  57. return
  58. if adjust_spines:
  59. plt.gca().spines['right'].set_color('none')
  60. plt.gca().spines['top'].set_color('none')
  61. plt.gca().xaxis.set_ticks_position('bottom')
  62. plt.gca().yaxis.set_ticks_position('left')
  63. if bw_friendly:
  64. setFigLinesBW(plt.gcf())
  65. setBarsBW(plt.gcf())
  66. if sci_y:
  67. # Change the Y axis to use a scientific notation and render it with LaTeX.
  68. formatter = matplotlib.ticker.ScalarFormatter(useMathText=True)
  69. formatter.set_powerlimits((-2,2))
  70. plt.gca().yaxis.set_major_formatter(formatter)
  71. # Following functions taken from:
  72. # https://stackoverflow.com/questions/7358118/matplotlib-black-white-colormap-with-dashes-dots-etc
  73. def setAxLinesBW(ax):
  74. """
  75. Take each Line2D in the axes, ax, and convert the line style to be
  76. suitable for black and white viewing.
  77. """
  78. marker_size = 4
  79. color_map = {
  80. '#1f77b4': {'marker': None, 'dash': (None,None)},
  81. '#ff7f0e': {'marker': None, 'dash': [2,1]},
  82. '#2ca02c': {'marker': None, 'dash': [3,1,1,1]},
  83. '#d62728': {'marker': None, 'dash': [3,1,1,1,1,1]},
  84. '#9467bd': {'marker': None, 'dash': [1,1]},
  85. '#8c564b': {'marker': None, 'dash': [4,1,2,0.5,0.3,0.5]},
  86. '#e377c2': {'marker': 'o', 'dash': (None,None)} #[1,2,1,10]}
  87. }
  88. lines_to_adjust = ax.get_lines()
  89. try:
  90. lines_to_adjust += ax.get_legend().get_lines()
  91. except AttributeError:
  92. pass
  93. for line in lines_to_adjust:
  94. orig_color = line.get_color()
  95. #line.set_color('black')
  96. try :
  97. line.set_dashes(color_map[orig_color]['dash'])
  98. line.set_marker(color_map[orig_color]['marker'])
  99. line.set_markersize(marker_size)
  100. except KeyError:
  101. print('Warning: could not add patterns to custom color "{}"'.format(orig_color))
  102. def setFigLinesBW(fig):
  103. """
  104. Take each axes in the figure, and for each line in the axes, make the
  105. line viewable in black and white.
  106. """
  107. for ax in fig.get_axes():
  108. setAxLinesBW(ax)
  109. def setBarsBW(fig):
  110. patterns = ['0', '///', '---', '|||', '+++', '**', 'oo', '...']
  111. inx = 0
  112. for ax in fig.get_axes():
  113. for child in ax.get_children():
  114. if isinstance(child, matplotlib.patches.Rectangle):
  115. child.set_hatch(patterns[inx])
  116. inx += 1
  117. if inx == len(patterns): inx = 0