Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当种类=';酒吧';但当种类=';第'行;_Python_Pandas_Matplotlib_Subplot - Fatal编程技术网

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=…
    部分,这确实很好,谢谢(投票)。和往常一样,我等了一会儿,然后接受了答案。