Home > OS >  writing embedded loops in seaborn to make histograms in separate windows and setting plot titles to
writing embedded loops in seaborn to make histograms in separate windows and setting plot titles to

Time:08-16

I'm trying to make histograms for PM2.5/PM10 ratios for 24 hour average data for around 50 sites and want the histograms to be in separate windows. I have a code that works, but it automatically sets the x-axis label as the site name. I figured out how to change the x labels but am having trouble writing a loop that changes the title name for each figure. Here's the part of the code that works:

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

for i,col in enumerate(pmf.columns):
    plt.figure(i)
    sns.histplot(pmf[col], binwidth=0.05, color='green')
    plt.xlim(0,1)
    plt.xlabel('PM$_{2.5}$/PM$_{10}$', fontsize=15)
    plt.ylabel('Frequency', fontsize=15)

Here is some sample data to work with: https://drive.google.com/file/d/1heF1W1x0qS_5SjjPg23j5alt2WiY3HsM/view?usp=sharing

I have trouble with embedding another loop that automatically changes the title based on a list of titles:

titles = ['1201 West Hwy 98', 'Betty Jo Mcneece Receiving Home',
       'Booker T. Washington Elementary', 'CCV', 'Calexico, 604 Kubler Rd',
       'Calexico, Alvarez', 'Calexico, Encinas Ave and Ethel St',
       'Calexico, Ethel', 'Calexico, Housing Authority',
       'Calexico, Housing Authority West', 'Calexico, Residence',
       'Dogwood Rd & HWY 98', 'El Centro West', 'El Centro, Wilson',
       'Frank Wright', "Gio's Mobile Home Estates", 'Heber',
       'Holtville, 1015 Miller Rd', 'Holtville, High School',
       'Holtville, South', 'Imperial County APCD', 'Kennedy', 'Meadows',
       'Ocotillo', 'Portico Blvd', 'Seeley', 'TL Waggoner',
       'Wozencraft Street & Acuña Ave']

I thought maybe I could add an additional variable to the first loop as so #for col, t in zip(enumerate(pmf.columns), titles):, but got an error message that said ValueError: not enough values to unpack (expected 3, got 2).

I tried this:

for i,col in enumerate(pmf.columns):
    plt.figure(i)
    sns.histplot(pmf[col], binwidth=0.05, color='green')
    plt.xlim(0,1)
    plt.xlabel('PM$_{2.5}$/PM$_{10}$', fontsize=15)
    plt.ylabel('Frequency', fontsize=15)
    for t in titles:
        plt.title(t, fontsize=20)

but this makes the title of all of the figures the last site name in the title list provided (Wozencraft...). I also tried

for t in titles:
    fig = plt.figure()
    plt.title(t, fontsize=20)
    for col in enumerate(pmf.columns):    
        fig.sns.histplot(pmf[col], binwidth=0.05, color='green')
        plt.xlim(0,1)
        plt.xlabel('PM$_{2.5}$/PM$_{10}$', fontsize=15)
        plt.ylabel('Frequency', fontsize=15)

but it created blank figures with a title and an error message that said AttributeError: 'Figure' object has no attribute 'sns'

If someone could help me, I will appreciate it.

CodePudding user response:

IIUC, you want to get histplots of each columns in titles. Then, you may prefer plt.subplots:

fig, ax = plt.subplots(len(titles))
fig.set_figheight(180)
fig.set_figwidth(15)

for i,col in enumerate(titles):
    ax[i].hist(pmf.iloc[:, i 1], color='green', width=0.05)
    ax[i].set(title=titles[i], xlim=[0,1])
    ax[i].set_xlabel(xlabel='PM$_{2.5}$/PM$_{10}$', fontsize=15)
    ax[i].set_ylabel(ylabel='Frequency', fontsize=15)

Note that since you set xlim=[0,1] some subplots are empty, also you may play around with fig.set_figheight() if there is overlapping on the axes

CodePudding user response:

I figured it out - I added this line of code to the loop and it worked:

plt.title(pmf.columns[i])
  • Related