paper_plots.py 5.1 KB

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