使用pandas dataframe.plot生成非堆叠面积图时,会得到比图例条目数量更多的彩色表面。
考虑一下:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(11, 3)+3, columns=['A', 'B', 'C'])使用
>>> print df给予例如:
A B C
0 1.908785 2.516292 4.139940
1 2.566306 3.275534 3.889655
2 2.083525 2.554483 3.565328
3 1.406931 2.021886 2.956590
4 3.293099 3.672927 3.203007
5 3.542735 1.301354 3.259613
6 1.331992 4.882820 2.165666
7 2.670735 3.763886 3.290484
8 4.211895 0.923923 3.415861
9 3.664398 2.009058 2.436214
10 2.707552 3.149282 1.629846和
df.plot(kind='area', stacked=False)制作:

对于数据帧中的三个数据系列或列,有七个不同颜色的表面:以A、B、C为基础,有AB、AC、BC对,以及所有的重叠: ABC。
尝试用重叠的圆圈在pyplot中可视化这一点,如下所示:
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
plt.figure()
circle1 = plt.Circle((3, 3), radius=3, fc='r', alpha=0.5, edgecolor=None)
circle2 = plt.Circle((3, 7), radius=3, fc='g', alpha=0.5, edgecolor=None)
circle3 = plt.Circle((6, 5), radius=3, fc='b', alpha=0.5, edgecolor=None)
circles = [circle1, circle2, circle3]
for cle in circles:
plt.gca().add_patch(cle)
plt.axis('scaled')
plt.xlim(0, 10)现在,我学习了如何使用line2D对象在pyplot中创建具有特定颜色的自定义图例,如下所示:
circ1 = Line2D([0], [0], linestyle='none', marker='s', alpha=0.5,
markersize=10, markerfacecolor='r')
circ2 = Line2D([0], [0], linestyle='none', marker='s', alpha=0.5,
markersize=10, markerfacecolor='g')
circ3 = Line2D([0], [0], linestyle='none', marker='s', alpha=0.5,
markersize=10, markerfacecolor="blue")
plt.legend((circ1, circ2, circ3), ('A', 'B', 'C'), numpoints=1, loc='best')产生以下输出:

但是,如何从未堆叠区域的原始熊猫图中获取重叠表面的确切颜色,从而提供一种创建具有七个条目的图例的方法?
还请注意,这里的颜色略有不同。一方面,在熊猫中,加法着色产生较深的红色(尽管这似乎随绘制的数据序列/数据帧的列的数量而变化),而另一方面,pyplot产生较深的蓝色。
发布于 2015-12-05 04:12:09
你可以手动计算混合颜色。例如,使用我找到的算法here (我使用了略有不同的alpha计算),我得到了如下内容:

为了更容易地将图例项与重叠圆的混合颜色进行比较,我将图例项at到图形中(圆圈边缘的小方块)。
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
plt.figure()
# cf = foreground color, cb = background color
def mix_colors(cf, cb):
a = cb[-1] + cf[-1] - cb[-1] * cf[-1] # fixed alpha calculation
r = (cf[0] * cf[-1] + cb[0] * cb[-1] * (1 - cf[-1])) / a
g = (cf[1] * cf[-1] + cb[1] * cb[-1] * (1 - cf[-1])) / a
b = (cf[2] * cf[-1] + cb[2] * cb[-1] * (1 - cf[-1])) / a
return [r,g,b,a]
c1 = [1.0, 0.1, 0.1, 0.5]
c2 = [0.3, 0.2, 0.7, 0.5]
c3 = [0.5, 0.8, 0.5, 0.5]
c12 = mix_colors(c2, c1) # mix c2 over c1
c13 = mix_colors(c3, c1) # mix c3 over c1
c123 = mix_colors(c3, c12) # mix c3 over c12
circle1 = plt.Circle((3, 3), radius=3, fc=c1, edgecolor=None)
circle2 = plt.Circle((3, 7), radius=3, fc=c2, edgecolor=None)
circle3 = plt.Circle((6, 5), radius=3, fc=c3, edgecolor=None)
circles = [circle1, circle2, circle3]
for cle in circles:
plt.gca().add_patch(cle)
plt.axis('scaled')
plt.xlim(0, 10)
circ1 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c1)
circ2 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c2)
circ3 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c3)
circ4 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c12)
circ5 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c13)
circ6 = Line2D([0], [0], linestyle='none', marker='s',
markersize=10, markerfacecolor=c123)
plt.legend((circ1, circ2, circ3, circ4, circ5, circ6), ('A', 'B', 'C', 'AB', 'AC', 'ABC'), numpoints=1, loc='best')https://stackoverflow.com/questions/34090694
复制相似问题