显然,这很容易使用
legendHandle = plt.legend(loc = "center left",bBox_to_anchor = (1,0.5))
但问题是图例被窗户边缘切断了.我花了很长时间寻找解决方案.到目前为止我能找到的最好的事情是:
Box = ax.get_position()ax.set_position([Box.x0,Box.y0,Box.wIDth * 0.8,Box.height])plt.legend(loc = "center left",0.5))
不幸的是,这并没有真正解决我的问题.由于应用于框宽的显式因子0.8,这仅适用于图形和图例宽度的一个特定组合.如果我调整图形窗口的大小,或者我的图例条目具有不同的长度,则它不起作用.
我只是不明白如何在图中放置一个图例是如此困难.我习惯了Matlab,就像它一样简单
legend('Location','eastoutsIDe')
Python中是否有类似的东西我缺少?
解决方法 尝试了很多,这是我能想到的最好的:from matplotlib.lines import line2Dfrom matplotlib.grIDspec import GrIDSpecfrom enum import Enumclass Location(Enum): EastOutsIDe = 1 WestOutsIDe = 2 northOutsIDe = 3 SouthOutsIDe = 4class Legend: def __init__(self,figure,plotAxes,location: Location): self.figure = figure self.plotAxes = plotAxes self.location = location # Create a separate subplot for the legend. Actual location doesn't matter - will be modifIEd anyway. self.legendAxes = figure.add_subplot(1,2,1) self.legendAxes.clear() # remove old lines self.legendAxes.set_axis_off() # Add all lines from the plot to the legend subplot for line in plotAxes.get_lines(): legendline = line2D([],[]) legendline.update_from(line) self.legendAxes.add_line(legendline) if self.location == Location.EastOutsIDe: self.legend = self.legendAxes.legend(loc = "center left") elif self.location == Location.WestOutsIDe: self.legend = self.legendAxes.legend(loc = "center right") elif self.location == Location.northOutsIDe: self.legend = self.legendAxes.legend(loc = "lower center") elif self.location == Location.southOutsIDe: self.legend = self.legendAxes.legend(loc = "upper center") else: raise Exception("UnkNown legend location.") self.UpdateSize() # Recalculate legend size if the size changes figure.canvas.mpl_connect('resize_event',lambda event: self.UpdateSize()) def UpdateSize(self): self.figure.canvas.draw() # draw everything once in order to get correct legend size # Extract legend size in percentage of the figure wIDth legendSize = self.legend.get_window_extent().inverse_transformed(self.figure.transfigure) legenDWIDth = legendSize.wIDth legendHeight = legendSize.height # Update subplot such that it is only as large as the legend if self.location == Location.EastOutsIDe: grIDspec = GrIDSpec(1,wIDth_ratios = [1 - legenDWIDth,legenDWIDth]) legendLocation = 1 plotLocation = 0 elif self.location == Location.WestOutsIDe: grIDspec = GrIDSpec(1,wIDth_ratios = [legenDWIDth,1 - legenDWIDth]) legendLocation = 0 plotLocation = 1 elif self.location == Location.northOutsIDe: grIDspec = GrIDSpec(2,1,height_ratios = [legendHeight,1 - legendHeight]) legendLocation = 0 plotLocation = 1 elif self.location == Location.southOutsIDe: grIDspec = GrIDSpec(2,height_ratios = [1 - legendHeight,legendHeight]) legendLocation = 1 plotLocation = 0 else: raise Exception("UnkNown legend location.") self.legendAxes.set_position(grIDspec[legendLocation].get_position(self.figure)) self.legendAxes.set_subplotspec(grIDspec[legendLocation]) # to make figure.tight_layout() work if that's desired self.plotAxes.set_position(grIDspec[plotLocation].get_position(self.figure)) self.plotAxes.set_subplotspec(grIDspec[plotLocation]) # to make figure.tight_layout() work if that's desired
在我到目前为止测试的情况下,这使得图例或多或少都没有.用法是例如
import matplotlib.pyplot as pltplt.ion()figure = plt.figure()plotAxes = figure.gca()plotAxes.plot([1,3],[4,5,6],"b-",label = "Testaaaaaaaaaaaaaa 1")plotAxes.plot([1,[6,4],"r-",label = "Test 2")legend = Legend(figure,Location.EastOutsIDe)
让我问一下我在评论中发布的问题……我将如何建议将其作为matplotlib开发人员的附加功能? (不是我的黑客,而是在图中有传说的本地方式)
总结以上是内存溢出为你收集整理的Python之外的传说 – matplotlib全部内容,希望文章能够帮你解决Python之外的传说 – matplotlib所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)