【问题标题】:Plot histograms in a loop and show them side by side [duplicate]在循环中绘制直方图并并排显示它们[重复]
【发布时间】:2018-04-05 22:34:03
【问题描述】:

我正在尝试绘制 n 个直方图并将它们并排显示(不在同一个直方图中。

我试过这段代码:

for r in range(1, n):
    plt.hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    plt.hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')
    plt.show()

当我输入此代码时,它会显示直方图 1,然后当我关闭图形时,会出现直方图 2,依此类推,直到直方图 n。

for r in range(1, n):
    plt.hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    plt.hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')
plt.show()

但是,当我尝试此代码时,它会显示一个直方图,其中包含所有直方图。

有没有办法在一个窗口中分别显示所有 n 个不同的直方图?

谢谢 :D

【问题讨论】:

标签: python matplotlib


【解决方案1】:

试试这个:

a = plt.subplots(n-1, 2)[1].ravel()

for r in range (1, n):
    a[0].hist(combined['apple{} tomato'.format(n)], bins = bin, alpha=0.5, color='#0bf9ea')
    a[1].hist(combined['apple{} potato'.format(n)], bins =  bin, alpha=0.5, color='#ff7fa7')

plt.show()

【讨论】:

    【解决方案2】:

    您是否尝试过使用 matplotlib.pyplot.subplot()?

     for r in range(1,n): 
        matplotlib.pyplot.subplot(nrows, ncols, r) 
        plt.hist() 
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多