【发布时间】:2014-06-26 16:39:48
【问题描述】:
怎么可能把图例放在情节之外?
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()
【问题讨论】:
-
实际上它不是完全重复的......这个问题是专门针对熊猫的。下面的响应显示了如何将轴分配给来自
pandas.DataFrame.plot的绘图函数调用,这使得应用matplotlib.pyplot改进成为可能。 -
是的,这不是重复的,它被标记为这样很烦人。
-
这不是重复的,因为它适用于 Pandas
.plot。感谢@matt_harrison,下面概述了解决方案,但总结一下:如果您有d.plot(kind='bar', ax=f.gca()),请将其更改为d.plot(kind='bar', ax=f.gca()).legend(bbox_to_anchor=(1,1)) -
看看这个人:stackoverflow.com/questions/4700614/… 它有很好的讨论/答案,如果你知道最小的 pandas/matplotlib,这个问题几乎是那个问题的副本。
标签: python matplotlib pandas