Guest User

Untitled

a guest
Oct 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. fig = plt.figure()
  5. ax1 = fig.add_subplot(111)
  6. ax2 = ax1.twinx()
  7.  
  8. ax1.bar(range(6), (2, -2, 1, 0, 0, 0))
  9. ax2.plot(range(6), (0, 2, 8, -2, 0, 0))
  10. plt.show()
  11.  
  12. import numpy as np
  13. import matplotlib.pyplot as plt
  14.  
  15. def align_yaxis(ax1, v1, ax2, v2):
  16. """adjust ax2 ylimit so that v2 in ax2 is aligned to v1 in ax1"""
  17. _, y1 = ax1.transData.transform((0, v1))
  18. _, y2 = ax2.transData.transform((0, v2))
  19. inv = ax2.transData.inverted()
  20. _, dy = inv.transform((0, 0)) - inv.transform((0, y1-y2))
  21. miny, maxy = ax2.get_ylim()
  22. ax2.set_ylim(miny+dy, maxy+dy)
  23.  
  24.  
  25. fig = plt.figure()
  26. ax1 = fig.add_subplot(111)
  27. ax2 = ax1.twinx()
  28.  
  29. ax1.bar(range(6), (2, -2, 1, 0, 0, 0))
  30. ax2.plot(range(6), (0, 2, 8, -2, 0, 0))
  31.  
  32. align_yaxis(ax1, 0, ax2, 0)
  33. plt.show()
  34.  
  35. def align_yaxis(ax1, v1, ax2, v2):
  36. """adjust ax2 ylimit so that v2 in ax2 is aligned to v1 in ax1"""
  37. _, y1 = ax1.transData.transform((0, v1))
  38. _, y2 = ax2.transData.transform((0, v2))
  39. adjust_yaxis(ax2,(y1-y2)/2,v2)
  40. adjust_yaxis(ax1,(y2-y1)/2,v1)
  41.  
  42. def adjust_yaxis(ax,ydif,v):
  43. """shift axis ax by ydiff, maintaining point v at the same location"""
  44. inv = ax.transData.inverted()
  45. _, dy = inv.transform((0, 0)) - inv.transform((0, ydif))
  46. miny, maxy = ax.get_ylim()
  47. miny, maxy = miny - v, maxy - v
  48. if -miny>maxy or (-miny==maxy and dy > 0):
  49. nminy = miny
  50. nmaxy = miny*(maxy+dy)/(miny+dy)
  51. else:
  52. nmaxy = maxy
  53. nminy = maxy*(miny+dy)/(maxy+dy)
  54. ax.set_ylim(nminy+v, nmaxy+v)
  55.  
  56. l1 = [0.03, -0.6, 1, 0.05]
  57. l2 = [0.8, 0.9, 1, 1.1]
  58. fig, ax1 = plt.subplots()
  59. ax1.plot(l1)
  60. ax2 = ax1.twinx()
  61. ax2.plot(l2, color='r')
  62. align_yaxis(ax1, 0, ax2, 0)
  63.  
  64. def align_yaxis(ax1, ax2):
  65. """Align zeros of the two axes, zooming them out by same ratio"""
  66. axes = (ax1, ax2)
  67. extrema = [ax.get_ylim() for ax in axes]
  68. tops = [extr[1] / (extr[1] - extr[0]) for extr in extrema]
  69. # Ensure that plots (intervals) are ordered bottom to top:
  70. if tops[0] > tops[1]:
  71. axes, extrema, tops = [list(reversed(l)) for l in (axes, extrema, tops)]
  72.  
  73. # How much would the plot overflow if we kept current zoom levels?
  74. tot_span = tops[1] + 1 - tops[0]
  75.  
  76. b_new_t = extrema[0][0] + tot_span * (extrema[0][1] - extrema[0][0])
  77. t_new_b = extrema[1][1] - tot_span * (extrema[1][1] - extrema[1][0])
  78. axes[0].set_ylim(extrema[0][0], b_new_t)
  79. axes[1].set_ylim(t_new_b, extrema[1][1])
  80.  
  81. def align_yaxis_np(ax1, ax2):
  82. """Align zeros of the two axes, zooming them out by same ratio"""
  83. axes = np.array([ax1, ax2])
  84. extrema = np.array([ax.get_ylim() for ax in axes])
  85. tops = extrema[:,1] / (extrema[:,1] - extrema[:,0])
  86. # Ensure that plots (intervals) are ordered bottom to top:
  87. if tops[0] > tops[1]:
  88. axes, extrema, tops = [a[::-1] for a in (axes, extrema, tops)]
  89.  
  90. # How much would the plot overflow if we kept current zoom levels?
  91. tot_span = tops[1] + 1 - tops[0]
  92.  
  93. extrema[0,1] = extrema[0,0] + tot_span * (extrema[0,1] - extrema[0,0])
  94. extrema[1,0] = extrema[1,1] + tot_span * (extrema[1,0] - extrema[1,1])
  95. [axes[i].set_ylim(*extrema[i]) for i in range(2)]
  96.  
  97. def align_yaxis_np(axes):
  98. """Align zeros of the two axes, zooming them out by same ratio"""
  99. axes = np.array(axes)
  100. extrema = np.array([ax.get_ylim() for ax in axes])
  101.  
  102. lowers = extrema[:, 0]
  103. uppers = extrema[:, 1]
  104.  
  105. all_positive = False
  106. all_negative = False
  107. if lowers.min() > 0.0:
  108. all_positive = True
  109.  
  110. if uppers.max() < 0.0:
  111. all_negative = True
  112.  
  113. if all_negative or all_positive:
  114. # don't scale
  115. return
  116. res = abs(uppers+lowers)
  117. min_index = np.argmin(res)
  118.  
  119. multiplier1 = abs(uppers[min_index]/lowers[min_index])
  120. multiplier2 = abs(lowers[min_index]/uppers[min_index])
  121.  
  122. for i in range(len(extrema)):
  123. if i == min_index:
  124. continue
  125. lower_change = extrema[i, 1] * -1*multiplier2
  126. upper_change = extrema[i, 0] * -1*multiplier1
  127. if extrema[i, 1] > upper_change:
  128. extrema[i, 0] = lower_change
  129. else:
  130. extrema[i, 1] = upper_change
  131. [axes[i].set_ylim(*extrema[i]) for i in range(len(extrema))]
Add Comment
Please, Sign In to add comment