如何用熊猫把传说放在情节之外

use*_*828 33 python matplotlib pandas

怎么可能把传说放在情节之外?

import pandas as pd
import matplotlib.pyplot as plt
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537},
    'Test2': {1: 23256313, 4: 21668216, 10: 19795367}}

d = pd.DataFrame(a).T
#print d

f = plt.figure()

plt.title('Title here!', color='black')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
d.plot(kind='bar', ax=f.gca())
plt.show()
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 58

我想你需要plot在添加呼叫之前打电话legend.

import pandas as pd
import matplotlib.pyplot as plt
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537},
    'Test2': {1: 23256313, 4: 21668216, 10: 19795367}}

d = pd.DataFrame(a).T
#print d

f = plt.figure()

plt.title('Title here!', color='black')
d.plot(kind='bar', ax=f.gca())
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.show()
Run Code Online (Sandbox Code Playgroud)

  • 如果你只想移动图例,你可以利用``.plot``返回matplotlib轴并在你的图的末尾添加``.legend(bbox_to_anchor =(1,1))``这一事实.代码行 (13认同)
  • @mattharrison的评论应该是答案. (5认同)
  • 我不认为这是一个单独的问题,重点是移动传说,我不明白为什么传说之后传说会突然变成半切. (4认同)
  • 然而,一半的传说现在不在图片中?怎么可能修复它? (2认同)

Cod*_*der 5

根据 OP 的问题,我可以使用以下代码段将图例放在图表之外:

import pandas as pd
import matplotlib.pyplot as plt
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537},
    'Test2': {1: 23256313, 4: 21668216, 10: 19795367}}

df = pd.DataFrame(a).T

ax = df.plot.bar()
ax.set_title("Title here!",color='black')
ax.legend(bbox_to_anchor=(1.0, 1.0))
ax.plot()
Run Code Online (Sandbox Code Playgroud)

它如何出现在我的笔记本中: 在此处输入图片说明

然后,您可以修改锚点值以根据需要调整其位置。锚点将是该图表的左下角。