python-使用loc重新定位我的图例

python-使用loc重新定位我的图例,python,pandas,matplotlib,legend,Python,Pandas,Matplotlib,Legend,这是我的代码的输出 如您所见,图例“pl”和“ppl”在右上角重叠。如何让其中一个移动到左上角。 我尝试搜索ans,并使用“loc”来修复问题,但不知何故,我继续得到错误。有人能帮忙吗 import numpy as np import pandas as pd import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(111) ax2 = ax1.twinx() ax1.set_xlabel('Date

这是我的代码的输出

如您所见,图例“pl”和“ppl”在右上角重叠。如何让其中一个移动到左上角。 我尝试搜索ans,并使用“loc”来修复问题,但不知何故,我继续得到错误。有人能帮忙吗

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel('Date')
ax1.set_ylabel('percent change / 100')
dd = pd.DataFrame(np.random.randint(1,10,(30,2)),columns=['pl','ppl'])
dd['pl'].plot(ax=ax1,legend=True)
dd['ppl'].plot(ax=ax2, style=['g--', 'b--', 'r--'],legend=True)

ax2.set_ylabel('difference')
plt.show()

可能直接使用matplotlib而不是使用数据框进行打印。打印:

ax1.plot(dd['pl'], label='pl')
ax1.legend(loc='upper left')
ax2.plot(dd['ppl'], ls='--', c='g', label='ppl')
ax2.legend(loc='upper right')
输出:


可能直接使用matplotlib打印,而不是使用
数据帧。打印

ax1.plot(dd['pl'], label='pl')
ax1.legend(loc='upper left')
ax2.plot(dd['ppl'], ls='--', c='g', label='ppl')
ax2.legend(loc='upper right')
输出:


我认为您需要调用
绘图上的
图例
,并相应地定位图例。请看下面

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel('Date')
ax1.set_ylabel('percent change / 100')
dd = pd.DataFrame(np.random.randint(1,10,(30,2)),columns=['pl','ppl'])

dd['pl'].plot(ax=ax1, legend=True).legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
dd['ppl'].plot(ax=ax2, style=['g--', 'b--', 'r--'],legend=True).legend(loc='upper right')

我认为您需要调用
绘图
上的
图例
,并相应地定位图例。请看下面

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel('Date')
ax1.set_ylabel('percent change / 100')
dd = pd.DataFrame(np.random.randint(1,10,(30,2)),columns=['pl','ppl'])

dd['pl'].plot(ax=ax1, legend=True).legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
dd['ppl'].plot(ax=ax2, style=['g--', 'b--', 'r--'],legend=True).legend(loc='upper right')

您可以通过多种方式创建图例:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel("Date")
ax1.set_ylabel("percent change / 100")
dd = pd.DataFrame(np.random.randint(1, 10, (30, 2)), columns=["pl", "ppl"])
dd["pl"].plot(ax=ax1)
dd["ppl"].plot(ax=ax2, style=["g--", "b--", "r--"])

# # two separate legends
# ax1.legend()
# ax2.legend(loc="upper left")

# # a single legend for the whole fig
# fig.legend(loc="upper right")

# # a single legend for the axis
# get the lines in the axis
lines1 = ax1.lines
lines2 = ax2.lines
all_lines = lines1 + lines2
# get the label for each line
all_labels = [lin.get_label() for lin in all_lines]
# place the legend
ax1.legend(all_lines, all_labels, loc="upper left")

ax2.set_ylabel("difference")
plt.show()
我未注释的最后一个在ax中创建了一个图例,并列出了两行


干杯

您可以通过多种方式创建图例:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_xlabel("Date")
ax1.set_ylabel("percent change / 100")
dd = pd.DataFrame(np.random.randint(1, 10, (30, 2)), columns=["pl", "ppl"])
dd["pl"].plot(ax=ax1)
dd["ppl"].plot(ax=ax2, style=["g--", "b--", "r--"])

# # two separate legends
# ax1.legend()
# ax2.legend(loc="upper left")

# # a single legend for the whole fig
# fig.legend(loc="upper right")

# # a single legend for the axis
# get the lines in the axis
lines1 = ax1.lines
lines2 = ax2.lines
all_lines = lines1 + lines2
# get the label for each line
all_labels = [lin.get_label() for lin in all_lines]
# place the legend
ax1.legend(all_lines, all_labels, loc="upper left")

ax2.set_ylabel("difference")
plt.show()
我未注释的最后一个在ax中创建了一个图例,并列出了两行


干杯

太好了,这就是我想要的!太好了,这就是我想要的!谢谢,但我想另一个解决方案更符合我的要求。谢谢,但我想另一个解决方案更符合我的要求。