我正试图在Seaborn FacetGrid中遮蔽两条线之间的区域。 fill_between
方法会执行此操作,但我需要访问每个子图中每行的值以将其传入。
这是我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = [{'Change': 0.0, 'Language': 'Algonquin', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Algonquin', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': -21.32, 'Language': 'Algonquin', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': -34.84, 'Language': 'Algonquin', 'Type': 'Spoken at home', 'Year': '2016'}, {'Change': 0.0, 'Language': 'Atikamekw', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Atikamekw', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': 5.41, 'Language': 'Atikamekw', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': 19.15, 'Language': 'Atikamekw', 'Type': 'Spoken at home', 'Year': '2016'}, {'Change': 0.0, 'Language': 'Blackfoot', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Blackfoot', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': -1.4, 'Language': 'Blackfoot', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': 61.42, 'Language': 'Blackfoot', 'Type': 'Spoken at home', 'Year': '2016'}, {'Change': 0.0, 'Language': 'Carrier', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Carrier', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': -20.38, 'Language': 'Carrier', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': -18.91, 'Language': 'Carrier', 'Type': 'Spoken at home', 'Year': '2016'}, {'Change': 0.0, 'Language': 'Chilcotin', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Chilcotin', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': -13.82, 'Language': 'Chilcotin', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': 7.41, 'Language': 'Chilcotin', 'Type': 'Spoken at home', 'Year': '2016'}, {'Change': 0.0, 'Language': 'Cree languages', 'Type': 'Mother tongue', 'Year': '2011'}, {'Change': 0.0, 'Language': 'Cree languages', 'Type': 'Spoken at home', 'Year': '2011'}, {'Change': -11.52, 'Language': 'Cree languages', 'Type': 'Mother tongue', 'Year': '2016'}, {'Change': 6.57, 'Language': 'Cree languages', 'Type': 'Spoken at home', 'Year': '2016'}]
langs = pd.DataFrame(data)
g = sns.FacetGrid(langs, col='Language', hue='Type', col_wrap = 4, size=2)
g.map(plt.plot, 'Year', 'Change').set_titles('{col_name}')
g.set(xticks=[2011, 2016], yticks = [-40, 0, 70] )
现在我如何访问每一行的值?我猜g.axes
的内容,但文档中没有任何内容可以帮助。
答案 0 :(得分:3)
FacetGrid的平面轴数组然后通过ax.lines
获取轴对象的所有线。使用get_xdata, get_ydata
调用来迭代这些行来请求行的数据,而不是使用这些数据。
示例代码:
...
for ax in g.axes.flat:
print (ax.lines)
for line in ax.lines:
print (line.get_xdata())
print (line.get_ydata())
输出您的代码数据:
[<matplotlib.lines.Line2D object at 0x10c5facc0>, <matplotlib.lines.Line2D object at 0x10c5fa940>]
['2011' '2016']
[ 0. -21.32]
['2011' '2016']
[ 0. -34.84]
[<matplotlib.lines.Line2D object at 0x10c39a160>, <matplotlib.lines.Line2D object at 0x10c5a4828>]
['2011' '2016']
[ 0. 5.41]
['2011' '2016']
[ 0. 19.15]
[<matplotlib.lines.Line2D object at 0x10c5ff6d8>, <matplotlib.lines.Line2D object at 0x10c67c630>]
['2011' '2016']
[ 0. -1.4]
['2011' '2016']
[ 0. 61.42]
[<matplotlib.lines.Line2D object at 0x10c637358>, <matplotlib.lines.Line2D object at 0x10c65ada0>]
['2011' '2016']
[ 0. -20.38]
['2011' '2016']
[ 0. -18.91]
[<matplotlib.lines.Line2D object at 0x10c613668>, <matplotlib.lines.Line2D object at 0x10c6134e0>]
['2011' '2016']
[ 0. -13.82]
['2011' '2016']
[ 0. 7.41]
[<matplotlib.lines.Line2D object at 0x10c5ffd30>, <matplotlib.lines.Line2D object at 0x10c4f5dd8>]
['2011' '2016']
[ 0. -11.52]
['2011' '2016']
[ 0. 6.57]
答案 1 :(得分:2)
在@Serenity's上进行扩展。使用行信息get_xdata, get_ydata
,您可以使用fill_between
之类的
g = sns.FacetGrid(langs, col='Language', hue='Type', col_wrap=4, size=3)
g.map(plt.plot, 'Year', 'Change').set_titles('{col_name}')
g.set(xticks=[2011, 2016], yticks = [-40, 0, 70] )
for ax in g.axes.flat:
ax.fill_between(ax.lines[0].get_xdata().astype(int),
ax.lines[0].get_ydata(0), ax.lines[1].get_ydata(),
facecolor='#ffdec1')