2020年8月16日 星期日

[ FAQ Matplotlib] How to put the legend out of the plot

 Source From Here

Question
I have a series of 20 plots (not subplots) to be made in a single figure. I want the legend to be outside of the box. At the same time, I do not want to change the axes, as the size of the figure gets reduced. Kindly help me for the following queries:
1. I want to keep the legend box outside the plot area. (I want the legend to be outside at the right side of the plot area).
2. Is there anyway that I reduce the font size of the text inside the legend box, so that the size of the legend box will be small.

How-To
You can make the legend text smaller by creating font properties:
  1. from matplotlib.font_manager import FontProperties  
  2.   
  3. fontP = FontProperties()  
  4. fontP.set_size('small')  
  5. legend([plot1], "title", prop=fontP)   
  6. # or add prop=fontP to whatever legend() call you already have  
Before you consider decreasing the font size (which can make things awfully hard to read), try playing around with placing the legend in different places. So, let's start with a generic example:
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. x = np.arange(10)  
  5.   
  6. fig = plt.figure()  
  7. ax = plt.subplot(111)  
  8.   
  9. for i in xrange(5):  
  10.     ax.plot(x, i * x, label='$y = %ix$' % i)  
  11.   
  12. ax.legend()  
  13.   
  14. plt.show()  


If we do the same thing, but use the bbox_to_anchor keyword argument we can shift the legend slightly outside the axes boundaries:
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. x = np.arange(10)  
  5.   
  6. fig = plt.figure()  
  7. ax = plt.subplot(111)  
  8.   
  9. for i in xrange(5):  
  10.     ax.plot(x, i * x, label='$y = %ix$' % i)  
  11.   
  12. ax.legend(bbox_to_anchor=(1.11.05))  
  13.   
  14. plt.show()  


Similarly, you can make the legend more horizontal and/or put it at the top of the figure (I'm also turning on rounded corners and a simple drop shadow):
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. x = np.arange(10)  
  5.   
  6. fig = plt.figure()  
  7. ax = plt.subplot(111)  
  8.   
  9. for i in xrange(5):  
  10.     line, = ax.plot(x, i * x, label='$y = %ix$'%i)  
  11.   
  12. ax.legend(loc='upper center', bbox_to_anchor=(0.51.05),  
  13.           ncol=3, fancybox=True, shadow=True)  
  14. plt.show()  


Alternatively, you can shrink the current plot's width, and put the legend entirely outside the axis of the figure (note: if you use tight_layout(), then leave out ax.set_position():
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. x = np.arange(10)  
  5.   
  6. fig = plt.figure()  
  7. ax = plt.subplot(111)  
  8.   
  9. for i in xrange(5):  
  10.     ax.plot(x, i * x, label='$y = %ix$'%i)  
  11.   
  12. # Shrink current axis by 20%  
  13. box = ax.get_position()  
  14. ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])  
  15.   
  16. # Put a legend to the right of the current axis  
  17. ax.legend(loc='center left', bbox_to_anchor=(10.5))  
  18.   
  19. plt.show()  


And in a similar manner, you can shrink the plot vertically, and put the a horizontal legend at the bottom:
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3.   
  4. x = np.arange(10)  
  5.   
  6. fig = plt.figure()  
  7. ax = plt.subplot(111)  
  8.   
  9. for i in xrange(5):  
  10.     line, = ax.plot(x, i * x, label='$y = %ix$'%i)  
  11.   
  12. # Shrink current axis's height by 10% on the bottom  
  13. box = ax.get_position()  
  14. ax.set_position([box.x0, box.y0 + box.height * 0.1,  
  15.                  box.width, box.height * 0.9])  
  16.   
  17. # Put a legend below current axis  
  18. ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),  
  19.           fancybox=True, shadow=True, ncol=5)  
  20.   
  21. plt.show()  


Have a look at the matplotlib legend guide. You might also take a look at plt.figlegend().

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...