gpt4 book ai didi

python - 海伯恩/Matplotlib : how to access line values in FacetGrid?

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:35 27 4
gpt4 key购买 nike

我正在尝试为 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] )

这会产生如下图表: enter image description here

现在如何访问每一行的值?我猜测 g.axes 有什么问题,但文档中的任何内容都没有帮助。

最佳答案

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]

关于python - 海伯恩/Matplotlib : how to access line values in FacetGrid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46248348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com