之前我曾问过一个问题,如何在不同的子图上绘制熊猫数据框中的不同列:Plot multiple lines on subplots with pandas df.plot,并得到了很好的答案。现在我正试图在情节上最大限度地利用空间,而传说证明是一个问题。我想做的是将3或4个系列放在一个图例上,剩下的放在另一个图例上,这样我就可以把它们放在一个角落里,它们很适合。
我尝试使用matplotlib描述的方法,如下所示:
from matplotlib.pyplot import *
p1, = plot([1,2,3], label="test1")
p2, = plot([3,2,1], label="test2")
l1 = legend([p1], ["Label 1"], loc=1)
l2 = legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.
gca().add_artist(l1) # add l1 as a separate artist to the axes
show()
但是,我遇到的问题要么是使用pandas df.plot,要么是试图在子图上实现。这是我尝试过的:
f, (ax1, ax2) = plt.subplots(ncols = 2)
p1 = dfcomb.iloc[:,:3].plot(ax=ax1, figsize=(14,5))
p2 = dfcomb.iloc[:,3:6].plot(ax=ax1, figsize=(14,5))
l1 = ax1.legend([p1], ["Label 1"], loc=1)
l2 = ax1.legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.
gca().add_artist(l1) # add l1 as a separate artist to the axes
这就是我得到的:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-d3206d8ce17d> in <module>()
15 l1 = ax1.legend([p1], ["Label 1"], loc=1)
16 l2 = ax1.legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.
---> 17 gca().add_artist(l1)
18
19 ax1.set_xlabel('Suction (cm)')
C:\Anaconda\lib\site-packages\matplotlib\axes\_base.pyc in add_artist(self, a)
1646 Returns the artist.
1647 """
-> 1648 a.axes = self
1649 self.artists.append(a)
1650 self._set_artist_props(a)
C:\Anaconda\lib\site-packages\matplotlib\artist.pyc in axes(self, new_axes)
235 if (new_axes is not None and
236 (self._axes is not None and new_axes != self._axes)):
--> 237 raise ValueError("Can not reset the axes. You are "
238 "probably trying to re-use an artist "
239 "in more than one Axes which is not "
ValueError: Can not reset the axes. You are probably trying to re-use an artist in more than one Axes which is not supported
任何人都有解决方法吗?
答案 0 :(得分:7)
你对gca()
的性质做出了错误的假设而遭到伏击。我也很惊讶,这就是为什么我决定添加一个答案(否则我们主要谈论一个错字级别的问题)。另外,我注意到这个问题与熊猫无关。
这是一个在没有熊猫的情况下重现问题的最小例子:
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(ncols = 2)
p1, = ax1.plot([1,2,3], label="test1")
p2, = ax1.plot([3,2,1], label="test2")
l1 = ax1.legend([p1], ["Label 1"], loc=1)
l2 = ax1.legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.
plt.gca().add_artist(l1)
那么问题是什么?仔细查看错误消息:
ValueError:无法重置轴。您可能正在尝试重新使用不支持的多个Axes 中的艺术家
(强调我的)。看:
>>> ax1
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83abf7e10>
>>> ax2
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83a992850>
>>> plt.gca()
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83a992850>
问题在于,即使您正在使用ax1
,“图形当前轴”(gca()
}也指向ax2
,Axes
创建最新。{ / p>
现在的解决方案很简单:明确重新绘制调用(记住,显式优于隐式):
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(ncols = 2)
p1, = ax1.plot([1,2,3], label="test1")
p2, = ax1.plot([3,2,1], label="test2")
l1 = ax1.legend([p1], ["Label 1"], loc=1)
l2 = ax1.legend([p2], ["Label 2"], loc=4) # this removes l1 from the axes.
ax1.add_artist(l1) # <-- just change here, refer to ax1 explicitly
它还活着!
如果你真的想使用df.plot
(一个便利功能)而不是控制你自己创建的情节,你必须做更多的工作。不幸的是df.plot
会返回它所绘制的Axes
对象(而不是图中包含的线对象列表),因此我们需要查看Axes
的子项以查找情节。上面的例子使用了数据帧:
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
# example input
df1 = pd.DataFrame({'test1': [1,2,3]})
df2 = pd.DataFrame({'test2': [3,2,1]})
f, (ax1, ax2) = plt.subplots(ncols = 2)
# disable automatic legends in order two have two separate legends
df1.plot(ax=ax1, legend=False)
df2.plot(ax=ax1, legend=False)
# ugly hack to grab the children of the created Axes
p1,p2 = [child for child in ax1.get_children()
if isinstance(child, matplotlib.lines.Line2D)]
# untangling the plots will be harder the more plots there are in the Axes
l1 = ax1.legend([p1], df1.columns, loc=1)
l2 = ax1.legend([p2], df2.columns, loc=4) # this removes l1 from the axes.
ax1.add_artist(l1) # <-- just change here, refer to ax1 explicitly