Python 当种类=';酒吧';但当种类=';第'行;
当我创建这样的子图时:Python 当种类=';酒吧';但当种类=';第'行;,python,pandas,matplotlib,subplot,Python,Pandas,Matplotlib,Subplot,当我创建这样的子图时: import pandas as pd import numpy as np import matplotlib.pyplot as plt import itertools df = pd.DataFrame(np.random.randn(20, 10), columns=list('ABCDEFGHIJ')) ax = df.plot(kind='bar', subplots=True, layout=(4, 3), sharex=True, sharey=Fa
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import itertools
df = pd.DataFrame(np.random.randn(20, 10), columns=list('ABCDEFGHIJ'))
ax = df.plot(kind='bar', subplots=True, layout=(4, 3), sharex=True, sharey=False)
for axi in itertools.chain.from_iterable(ax):
try:
axi.set_title(axi.get_legend_handles_labels()[1][0], {'size': 16})
except:
pass
我得到这样一个图,其中列标题用作子图的标题:
当我将kind='bar'
更改为kind='line'
时,只绘制了图例,而没有绘制标题
解决方法是使用。获取\u图例\u句柄\u标签()
,如下所示:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import itertools
df = pd.DataFrame(np.random.randn(20, 10), columns=list('ABCDEFGHIJ'))
ax = df.plot(kind='bar', subplots=True, layout=(4, 3), sharex=True, sharey=False)
for axi in itertools.chain.from_iterable(ax):
try:
axi.set_title(axi.get_legend_handles_labels()[1][0], {'size': 16})
except:
pass
然后给出正确的输出:
有没有更直接的方法来实现这一点?为什么plot
的行为会因标志kind
的不同而不同?解决方法:
绘图
的标题
参数提供列表李>
ax = df.plot(kind="line",subplots=True, layout=(4, 3),
sharex=True, sharey=False, title=list(df.columns.values))
以获取子地块标题当然,您也可以直接尝试更改代码以包括轴标题的设置。在当前的开发版本中,条形图的代码大约在第2006行;您需要将其放置在线条周围某个位置的线条图的
\u make\u plot()
方法中,在该方法中,一个简单的ax.set\u title(label)
就足够了。问题可能是熊猫会要求你反驳它。(我还没有测试过)。我会在熊猫虫追踪系统上对此提出一个问题。在我看来似乎有点不一致。@Langitar好的,我会等一会儿,看看是否有人知道发生了什么。关于我可以使用的解决方法有什么建议吗?是的,你肯定应该在上提出一个问题。我认为在中添加ax.set\u title(label)
就足够了。好的,我复制了整个文件并使用了title=…
部分,这确实很好,谢谢(投票)。和往常一样,我等了一会儿,然后接受了答案。