【问题标题】:How to plot a histogram and describe it side-by-side in python?如何绘制直方图并在python中并排描述它?
【发布时间】:2019-07-20 13:51:33
【问题描述】:

以下代码生成我的数据集的描述,加上一个直方图,一个在另一个之上...

get_ipython().magic(u'matplotlib inline')
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(dfx.hits, bins=20)

dfx.hits.describe()

(而且不是我期望的顺序。)

结果如下:

但我希望这些数字并排显示。这可能吗?

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    詹姆斯关于使用子图的建议很有帮助,但不如这个问题提供的答案那么详细:

    How to Display Dataframe next to Plot in Jupyter Notebook

    Charles Parr 的回答帮助我编写了这段代码:

    fig = plt.figure(figsize=(10,4))
    
    ax1 = fig.add_subplot(121)
    ax1.hist(dfx.hits, bins=20, color='black')
    ax1.set_title('hits x users histogram')
    
    dfxx = dfx.hits.describe().to_frame().round(2)
    
    ax2 = fig.add_subplot(122)
    font_size=12
    bbox=[0, 0, 1, 1]
    ax2.axis('off')
    mpl_table = ax2.table(cellText = dfxx.values, rowLabels = dfxx.index, bbox=bbox, colLabels=dfxx.columns)
    mpl_table.auto_set_font_size(False)
    mpl_table.set_fontsize(font_size)
    

    生成直方图和描述的并排子图:

    【讨论】:

      【解决方案2】:

      您将要使用 matplotlib 的内置子绘图来制作 (2,1) 绘图并将第一个绘图放置在 (0,0) 和第二个绘图 (1,0)。 你可以找到官方文档here 这不是很好。

      我建议您查看 python-course.eu here 上的指南:

      另外它乱序的原因是因为你没有调用 plt.show() 或 seaborn 等价物,因此 ipython 最后吐出图形,因为它被调用而没有被分配到其他地方。

      类似于如果你有:

      import matplotlib.pyplot as plt
      
      plt.plot(range(10),range(10))
      print("hello")
      

      输出是:

      >>hello
      >>'Some Graph here'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-17
        • 2018-04-05
        • 2021-03-18
        • 1970-01-01
        • 2023-01-03
        • 1970-01-01
        • 2018-10-26
        • 2016-11-01
        相关资源
        最近更新 更多