0

我不确定为什么会发生这种情况。也许这只是一个我看不到的简单错误,但是通过使用以下代码:

for filename in glob.glob('/Users/jacob/Desktop/MERS/new/NOT COAL/gensets/statistics_per_lgu/per_lgu_files/*.csv'):
    base = os.path.basename(filename)
    name = os.path.splitext(base)[0]
    df = pd.read_csv(filename)

    # Show 4 different binwidths
    for i, binwidth in enumerate([10, 20, 30, 40]):
        # Set up the plot
        ax = plt.subplot(2, 2, i + 1)

        plt.subplots_adjust( wspace=0.5, hspace=0.5)

        # Draw the plot
        ax.hist(df['New Capacity based on 0.8 PF'], bins=binwidth,
                color='red', edgecolor='black',alpha=0.5)

        # Title and labels
        ax.set_title('Histogram with Binwidth = %d' % binwidth, size=10)
        ax.set_xlabel('Capacity', size=11)
        ax.set_ylabel('Frequency count', size=11)

        ax.axvline(x=df['New Capacity based on 0.8 PF'].median(), linestyle='dashed', alpha=0.3, color='blue')
        min_ylim, max_ylim = plt.ylim()
        ax.text(x=df['New Capacity based on 0.8 PF'].median(),y= max_ylim*0.9, s='Median', alpha=0.7, color='blue',fontsize = 12)

        ax.axvline(x=df['New Capacity based on 0.8 PF'].mean(), linestyle='dashed', alpha=0.9, color='green')
        min_ylim, max_ylim = plt.ylim()
        ax.text(x=df['New Capacity based on 0.8 PF'].mean(),y= max_ylim*0.5, s='Mean', alpha=0.9, color='green',fontsize = 12)

        plt.tight_layout()
        plt.grid(True)
        plt.savefig('/Users/jacob/Documents/Gensets_gis/historgrams/per_lgu_files/{}.png'.format(name))

我在这里获得了像这张附加照片一样创建的所有文件。

关于我做错了什么有什么想法吗?

提前致谢。 附一张直方图输出的照片

我想要的结果是这样的。

期望的输出

4

1 回答 1

0

它不会创建新的子图,但会使用以前的子图,然后在旧图上绘制新图,因此您必须在绘制新直方图之前使用清晰的子图。

ax = plt.subplot(2, 2, i + 1)
ax.clear()

示例代码。它提供了所需的输出,但如果您删除 `ax.clear() 那么第一张图像会没问题,但您会在第二张和第三张图像上获得带有旧图的新图。

import os
import pandas as pd
import matplotlib.pyplot as plt
import random

for n in range(3):
    filename = f'example_data_{n}.csv'
    base = os.path.basename(filename)
    name = os.path.splitext(base)[0]

    df = pd.DataFrame({'New Capacity based on 0.8 PF': random.choices(list(range(1000)), k=100)})

    data = df['New Capacity based on 0.8 PF']
    median = data.median()
    mean = data.mean()


    # Show 4 different binwidths
    for i, binwidth in enumerate([10, 20, 30, 40]):
        # Set up the plot
        ax = plt.subplot(2,2,i+1)

        ax.clear()  # <--- it removes previous histogram 

        plt.subplots_adjust( wspace=0.5, hspace=0.5)

        # Draw the plot
        ax.hist(data , bins=binwidth, color='red', edgecolor='black',alpha=0.5)

        # Title and labels
        ax.set_title('Histogram with Binwidth = %d' % binwidth, size=10)
        ax.set_xlabel('Capacity', size=11)
        ax.set_ylabel('Frequency count', size=11)

        min_ylim, max_ylim = plt.ylim()

        ax.axvline(x=median, linestyle='dashed', alpha=0.3, color='blue')
        ax.text(x=median, y= max_ylim*0.9, s='Median', alpha=0.7, color='blue',fontsize = 12)

        ax.axvline(x=mean, linestyle='dashed', alpha=0.9, color='green')
        ax.text(x=mean, y= max_ylim*0.5, s='Mean', alpha=0.9, color='green',fontsize = 12)

        plt.tight_layout()
        plt.grid(True)

    plt.savefig('{}.png'.format(name))
于 2019-09-21T05:40:55.743 回答