python中plot画图图例_Python matplotlib中绘图外的图例

经过多次尝试,这是我能想到的最好的:from matplotlib.lines import Line2D

from matplotlib.gridspec import GridSpec

from enum import Enum

class Location(Enum):

EastOutside = 1

WestOutside = 2

NorthOutside = 3

SouthOutside = 4

class 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, 2, width_ratios = [1 - legendWidth, legendWidth])

legendLocation = 1

plotLocation = 0

elif self.location == Location.WestOutside:

gridspec = GridSpec(1, 2, 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, 1, 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

在我迄今为止测试过的案例中,这个传说或多或少是好的。用法是

^{pr2}$

让我问一个问题,我已经在一个评论。。。对于matplotlib开发人员,我该如何建议将其作为一个附加特性呢?(不是我的黑客,而是一种在人物之外拥有传奇的本土方式)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matplotlib是一个流行的Python可视化库,它可以用来绘制各种类型的图表,包括线图、散点图、柱状图、饼图等等。在本文,我们将介绍如何使用Matplotlib绘制多个变量的图表。 首先,我们需要导入Matplotlib库,并生成一些数据用于绘图。例如,我们可以使用NumPy库生成一些随机数据: ```python import matplotlib.pyplot as plt import numpy as np # 生成数据 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) ``` 接下来,我们可以使用Matplotlibplot函数绘制两个不同的线图: ```python # 绘制两个线图 plt.plot(x, y1, label='sin') plt.plot(x, y2, label='cos') # 添加图例 plt.legend() # 显示图表 plt.show() ``` 这将生成一个包含两个线图的图表,并在图例显示每个线的标签。 如果我们想要绘制一个包含多个变量的图表,我们可以使用Matplotlib的subplot函数创建多个子图。例如,我们可以使用如下代码创建一个包含两个子图的图表: ```python # 创建包含两个子图的图表 fig, axs = plt.subplots(2) # 在第一个子图绘制sin函数 axs[0].plot(x, y1) axs[0].set_title('sin') # 在第二个子图绘制cos函数 axs[1].plot(x, y2) axs[1].set_title('cos') # 调整子图之间的间距 plt.tight_layout() # 显示图表 plt.show() ``` 这将生成一个包含两个子图的图表,每个子图都包含一个变量的图表。我们可以通过调整subplot函数的参数来创建包含更多子图的图表。 除了使用subplot函数,Matplotlib还提供了许多其他的绘图函数和工具,可以帮助我们创建各种类型的图表。例如,我们可以使用scatter函数创建散点图,使用bar函数创建柱状图,使用pie函数创建饼图等等。Matplotlib的文档包含了详细的说明和示例,可以帮助我们学习如何使用这些功能来绘制各种类型的图表。 总之,Matplotlib是一个功能强大的Python可视化库,可以帮助我们创建各种类型的图表。通过使用subplot函数和其他绘图函数,我们可以轻松地绘制包含多个变量的图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值