Python 为具有不同列的子地块创建唯一图例
我必须从两个df中画出两个子图,它们有不同的列。我希望得到包含这两个列的所有列的通用图例,类似于下面的示例Python 为具有不同列的子地块创建唯一图例,python,pandas,matplotlib,plot,visualization,Python,Pandas,Matplotlib,Plot,Visualization,我必须从两个df中画出两个子图,它们有不同的列。我希望得到包含这两个列的所有列的通用图例,类似于下面的示例 d1 = pd.DataFrame({ #without one 'two' : [-1.,- 2.,- 3., -4.], 'three' : [4., 3., 2., 1.], 'four' : [4., 3., 4., 3.]}) tot_1=d1.sum(axis=1) d2 = pd.DataFrame({'one' : [1., 2., 3., 4.]
d1 = pd.DataFrame({ #without one
'two' : [-1.,- 2.,- 3., -4.],
'three' : [4., 3., 2., 1.],
'four' : [4., 3., 4., 3.]})
tot_1=d1.sum(axis=1)
d2 = pd.DataFrame({'one' : [1., 2., 3., 4.],
'two' : [4., 3., 3., 1.],
'three' : [-1., -1., -3., -4.],
'four' : [4., 3., 2., 1.]})
tot_2=d2.sum(axis=1)
fig, ax = plt.subplots(nrows=2, ncols=1)
#plot 1
d1.plot.area(stacked=True,legend=False,ax=ax[0])
tot_1.plot(linestyle='-', color='black',legend=False,ax=ax[0])
###SECOND GRAPH####
ax3 = ax[1].twiny()
#plot 2
d2.plot.area(stacked=True,legend=False,ax=ax[1],sharex=ax[0])
tot_2.plot(linestyle='-',color='black',legend=False,ax=ax[1])
plt.show()
问题在于,两个数据帧/绘图中的列不完全相同(有些不相同),应确保图例具有两个绘图中的所有列和颜色,并且图例匹配
如果我能为每一列选择颜色,那就更好了(但不是必须的),例如,使用带有
列名:color
的字典来传递您确实可以使用带有列名:color
对的字典来为补丁着色,然后从中创建一个图例
import pandas as pd
import matplotlib.pyplot as plt
d1 = pd.DataFrame({ #without one
'two' : [-1.,- 2.,- 3., -4.],
'three' : [4., 3., 2., 1.],
'four' : [4., 3., 4., 3.]})
tot_1=d1.sum(axis=1)
d2 = pd.DataFrame({'one' : [1., 2., 3., 4.],
'two' : [4., 3., 3., 1.],
'three' : [-1., -1., -3., -4.],
'four' : [4., 3., 2., 1.]})
tot_2=d2.sum(axis=1)
columns = ["one", "two", "three", "four"]
colors = dict(zip(columns, ["C"+str(i) for i in range(len(columns)) ]))
fig, ax = plt.subplots(nrows=2, ncols=1)
#plot 1
d1.plot.area(stacked=True,legend=False,ax=ax[0], lw=0,
color=[colors[i] for i in d1.columns])
tot_1.plot(linestyle='-', color='black',legend=False,ax=ax[0])
###SECOND GRAPH####
ax3 = ax[1].twiny()
#plot 2
d2.plot.area(stacked=True,legend=False,ax=ax[1],sharex=ax[0], lw=0,
color=[colors[i] for i in d2.columns])
tot_2.plot(linestyle='-',color='black',legend=False,ax=ax[1])
labels = list(set(list(d1.columns) + list(d2.columns)))
handles = [plt.Rectangle((0,0),1,1, color=colors[l]) for l in labels]
ax3.legend(handles=handles, labels=labels)
plt.show()
您确实可以使用
列名:color
对字典对补丁进行着色,然后从中创建图例
import pandas as pd
import matplotlib.pyplot as plt
d1 = pd.DataFrame({ #without one
'two' : [-1.,- 2.,- 3., -4.],
'three' : [4., 3., 2., 1.],
'four' : [4., 3., 4., 3.]})
tot_1=d1.sum(axis=1)
d2 = pd.DataFrame({'one' : [1., 2., 3., 4.],
'two' : [4., 3., 3., 1.],
'three' : [-1., -1., -3., -4.],
'four' : [4., 3., 2., 1.]})
tot_2=d2.sum(axis=1)
columns = ["one", "two", "three", "four"]
colors = dict(zip(columns, ["C"+str(i) for i in range(len(columns)) ]))
fig, ax = plt.subplots(nrows=2, ncols=1)
#plot 1
d1.plot.area(stacked=True,legend=False,ax=ax[0], lw=0,
color=[colors[i] for i in d1.columns])
tot_1.plot(linestyle='-', color='black',legend=False,ax=ax[0])
###SECOND GRAPH####
ax3 = ax[1].twiny()
#plot 2
d2.plot.area(stacked=True,legend=False,ax=ax[1],sharex=ax[0], lw=0,
color=[colors[i] for i in d2.columns])
tot_2.plot(linestyle='-',color='black',legend=False,ax=ax[1])
labels = list(set(list(d1.columns) + list(d2.columns)))
handles = [plt.Rectangle((0,0),1,1, color=colors[l]) for l in labels]
ax3.legend(handles=handles, labels=labels)
plt.show()
你看到这个答案了吗(编辑的答案)这里有一个类似(但不同)的解决方案你看到这个答案了吗(编辑的答案)这里有一个类似(但不同)的解决方案