在Pandas中使用Plot()函数并拥有两个y轴:第二条线存储在哪里?

huangapple go评论95阅读模式
英文:

Plot() in Pandas with two y-axes: Where is the second line stored?

问题

I am using the integrated plot() function in pandas to generate a graph with two y-axes. This works well and the legend even points to the (right) y-axis for the second data set. But imho the legend's position is bad.

然而,当我更新图例位置时,我得到两个图例,一个正确的('A','B(右)')在一个不便利的位置,一个错误的('A')在所选位置。

So now I want to generate a legend on my own and was looking for the second <matplotlib.lines.Line2D>, but it is not contained in the ax environment.

所以现在我想自己生成一个图例,并在寻找第二个 <matplotlib.lines.Line2D>,但它不包含在 ax 环境中。

import pandas as pd
df = pd.DataFrame({"A":[1,2,3],"B":[1/4,1/5,1/6]})
ax = df.plot(secondary_y=['B'])
len(ax.lines)
>>> 1

My ultimate objective is to be able to move the correct legend around, but I am confident I could manually place a legend, if only I had access to the second line container.

我的最终目标是能够移动正确的图例,但我相信如果我能访问第二个线容器,我可以手动放置一个图例。

If I had, I was going to suppress the original legend by invoking df.plot(...,legend=None) and do something like plt.legend([ax.lines[0],ax.lines[1]],['A','B (right)'],loc='center left',bbox_to_anchor=(1.2, 0.5)). But ax only stores the first line "A", where is the second?

如果我有的话,我打算通过调用 df.plot(...,legend=None) 来抑制原始图例,并做类似 plt.legend([ax.lines[0],ax.lines[1]],['A','B (right)'],loc='center left',bbox_to_anchor=(1.2, 0.5)) 这样的操作。但是 ax 只存储第一条线 "A",第二条在哪里?

Also ax.get_legend_handles_labels() only contains ([<matplotlib.lines.Line2D at 0x2630e2193c8>], ['A']).

此外,ax.get_legend_handles_labels() 只包含 ([<matplotlib.lines.Line2D at 0x2630e2193c8>], ['A'])

英文:

I am using the integrated plot() function in pandas to generate a graph with two y-axes. This works well and the legend even points to the (right) y-axis for the second data set. But imho the legend's position is bad.

However, when I update the legend position I get two legends the correct one ('A', 'B (right)') at an inconvenient location, and a wrong one ('A' only) at the chosen location.

So now I want to generate a legend on my own and was looking for the second &lt;matplotlib.lines.Line2D&gt;, but it is not contained in the ax environment.

import pandas as pd
df = pd.DataFrame({&quot;A&quot;:[1,2,3],&quot;B&quot;:[1/4,1/5,1/6]})
ax = df.plot(secondary_y=[&#39;B&#39;])
len(ax.lines)
&gt;&gt;&gt; 1

My ultimate objective is to be able to move the correct legend around, but I am confident I could manually place a legend, if only I had access to the second line container.

If I had, I was going to suppress the original legend by invoking df.plot(...,legend=None) and do something like plt.legend([ax.lines[0],ax.lines[1]],[&#39;A&#39;,&#39;B (right)&#39;],loc=&#39;center left&#39;,bbox_to_anchor=(1.2, 0.5)). But ax only stores the first line "A", where is the second?

Also ax.get_legend_handles_labels() only contains ([&lt;matplotlib.lines.Line2D at 0x2630e2193c8&gt;], [&#39;A&#39;]).

答案1

得分: 3

你创建了两个坐标轴。每个坐标轴包含一条线。因此,您需要循环遍历这些坐标轴,并获取每个坐标轴上的线条。

import numpy as np
import pandas as pd

df = pd.DataFrame({"A":[1,2,3],"B":[1/4,1/5,1/6]})
ax = df.plot(secondary_y=['B'])

lines = np.array([axes.lines for axes in ax.figure.axes]).flatten()
print(lines)

为了创建一个单一的图例,您可以使用图例功能:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"A":[1,2,3],"B":[1/4,1/5,1/6]})
ax = df.plot(secondary_y=['B'], legend=False)

ax.figure.legend()
plt.show()
英文:

You create two axes. Each contains a line. So you need to loop over the axes and take the line(s) from each of them.

import numpy as np
import pandas as pd

df = pd.DataFrame({&quot;A&quot;:[1,2,3],&quot;B&quot;:[1/4,1/5,1/6]})
ax = df.plot(secondary_y=[&#39;B&#39;])

lines = np.array([axes.lines for axes in ax.figure.axes]).flatten()
print(lines)

For the purpose of creating a single legend you may however just use a figure legend,

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({&quot;A&quot;:[1,2,3],&quot;B&quot;:[1/4,1/5,1/6]})
ax = df.plot(secondary_y=[&#39;B&#39;], legend=False)

ax.figure.legend()
plt.show()

huangapple
  • 本文由 发表于 2020年1月7日 01:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定