paper_plots.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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'] = 10
  35. plt.rcParams['font.family'] = 'Computer Modern'
  36. plt.rcParams['axes.labelsize'] = 8
  37. plt.rcParams['axes.titlesize'] = 9
  38. plt.rcParams['legend.fontsize'] = 9
  39. plt.rcParams['xtick.labelsize'] = 7
  40. plt.rcParams['ytick.labelsize'] = 7
  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. plt.rcParams['hatch.linewidth'] = 1
  56. def post_paper_plot(change=True, bw_friendly=False, adjust_spines=False, sci_y=False):
  57. if not change:
  58. return
  59. if adjust_spines:
  60. plt.gca().spines['right'].set_color('none')
  61. plt.gca().spines['top'].set_color('none')
  62. plt.gca().xaxis.set_ticks_position('bottom')
  63. plt.gca().yaxis.set_ticks_position('left')
  64. if bw_friendly:
  65. setFigLinesBW(plt.gcf())
  66. setBarsBW(plt.gcf())
  67. if sci_y:
  68. # Change the Y axis to use a scientific notation and render it with LaTeX.
  69. formatter = matplotlib.ticker.ScalarFormatter(useMathText=True)
  70. formatter.set_powerlimits((-2,2))
  71. plt.gca().yaxis.set_major_formatter(formatter)
  72. # Following functions taken from:
  73. # https://stackoverflow.com/questions/7358118/matplotlib-black-white-colormap-with-dashes-dots-etc
  74. def setAxLinesBW(ax):
  75. """
  76. Take each Line2D in the axes, ax, and convert the line style to be
  77. suitable for black and white viewing.
  78. """
  79. marker_size = 4
  80. color_map = {
  81. '#1f77b4': {'marker': None, 'dash': (None,None)},
  82. '#ff7f0e': {'marker': None, 'dash': [2,1]},
  83. '#2ca02c': {'marker': None, 'dash': [3,1,1,1]},
  84. '#d62728': {'marker': None, 'dash': [3,1,1,1,1,1]},
  85. '#9467bd': {'marker': None, 'dash': [1,1]},
  86. '#8c564b': {'marker': None, 'dash': [4,1,2,0.5,0.3,0.5]},
  87. '#e377c2': {'marker': 'o', 'dash': (None,None)} #[1,2,1,10]}
  88. }
  89. lines_to_adjust = ax.get_lines()
  90. try:
  91. lines_to_adjust += ax.get_legend().get_lines()
  92. except AttributeError:
  93. pass
  94. for line in lines_to_adjust:
  95. orig_color = line.get_color()
  96. #line.set_color('black')
  97. try :
  98. line.set_dashes(color_map[orig_color]['dash'])
  99. line.set_marker(color_map[orig_color]['marker'])
  100. line.set_markersize(marker_size)
  101. except KeyError:
  102. print('Warning: could not add patterns to custom color "{}"'.format(orig_color))
  103. def setFigLinesBW(fig):
  104. """
  105. Take each axes in the figure, and for each line in the axes, make the
  106. line viewable in black and white.
  107. """
  108. for ax in fig.get_axes():
  109. setAxLinesBW(ax)
  110. def setBarsBW(fig):
  111. patterns = ['///', '---', '|||', '+++', '**', 'oo', '...']
  112. inx = 0
  113. boxes = []
  114. for ax in fig.get_axes():
  115. for child in ax.get_children():
  116. if isinstance(child, matplotlib.patches.Rectangle):
  117. boxes.append(child)
  118. # Skip the last rectangle which should be the background
  119. # Keep the same BW pattern when bars have the same color
  120. colors = dict()
  121. for box in boxes[:-1]:
  122. box_color = box.get_facecolor()
  123. #print(box_color)
  124. if(box_color in colors):
  125. box.set_hatch(colors[box_color])
  126. else:
  127. box.set_hatch(patterns[inx])
  128. colors[box_color] = patterns[inx]
  129. inx += 1
  130. if inx == len(patterns): inx = 0