关于Pandas df.plot子图的多个传说?

spi*_*hiz 6 python matplotlib python-2.7 pandas

之前我曾经问过一个关于如何在不同的子图上绘制熊猫数据框中的不同列的问题:用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()
Run Code Online (Sandbox Code Playgroud)

但是,我遇到的问题要么是使用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
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

---------------------------------------------------------------------------
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
Run Code Online (Sandbox Code Playgroud)

任何人都有解决方法吗?

And*_*eak 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)
Run Code Online (Sandbox Code Playgroud)

所以有什么问题?仔细查看错误消息:

ValueError:无法重置轴.您可能正在尝试在多个不受支持的Axes中重复使用艺术家

(强调我的).看:

>>> ax1
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83abf7e10>
>>> ax2
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83a992850>
>>> plt.gca()
<matplotlib.axes._subplots.AxesSubplot at 0x7fd83a992850>
Run Code Online (Sandbox Code Playgroud)

问题是,即使你正在努力ax1,"图形当前轴"也gca()指向ax2,Axes创建的最新.

解决方案现在很简单:使用重绘调用显式(请记住,显式优于隐式):

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
Run Code Online (Sandbox Code Playgroud)

它还活着!

结果


如果你真的想使用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
Run Code Online (Sandbox Code Playgroud)