Plotting with List Comprehensions

Hello!

I just finished this project in the Mastering Stats with Python course here on Codecademy. One of the extensions for this project is to attempt to use a list compression rather than a for loop to plot the given data. However, the code I have in the for loop, as seen below, seems like a whole lot to cram into a single list comprehension! Does anyone have any pointers on how to think about attempting this?

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import codecademylib3

# load in the data
df = pd.read_csv("mushroom_data.csv")
print(df.head())

# list of all column headers
columns = df.columns.tolist()

# loop traversing each coloumn 
for column in columns: 
  # print(column)
  sns.countplot(df[column], order=df[column].value_counts().index)
  # rotates the value labels slightly so the don’t overlap, also slightly increases font size
  plt.xticks(rotation=30, fontsize=10)
  # increases the variable label font size slightly to increase readability
  plt.xlabel(column, fontsize=12)
  plt.title(column + ' Value Counts')
  plt.show()
  plt.clf()

To be honest, that challenge doesn’t make any sense, so I wouldn’t worry about it.

List comprehensions are for when you are trying to create a list. Here, you’re not trying to create a list, you’re trying to plot data from (roughly) a 2d list using a series of steps. I don’t even think you could do all of that with a list comprehension—and even if you could, it would be much harder to read.

The only part here where it might make sense to use list comprehension is when you make your columns variable.

3 Likes

Does this work? It may not make sense to organize it like this, but I was curious. I haven’t learned much about graphing yet.

def graphing(column):
  # print(column)
  sns.countplot(df[column], order=df[column].value_counts().index)
  # rotates the value labels slightly so the don’t overlap, also slightly increases font size
  plt.xticks(rotation=30, fontsize=10)
  # increases the variable label font size slightly to increase readability
  plt.xlabel(column, fontsize=12)
  plt.title(column + ' Value Counts')
  plt.show()
  plt.clf()

output = [graphing(column) for column in columns]
3 Likes

You could make it work like that but anyone who actually did so would probably have to wear a cone of shame or the equivalent for the most un-pythonic act of the day :laughing:. I’d suggest avoiding any side effects in the expression of a list comprehension, let alone designing one purely for that purpose :scream:.

2 Likes

Hi, beginner here. After a bit of trial and error, here you go:

[[sns.countplot(df[col]), plt.show(), plt.clf()] for col in columns]

I don’t see this hard to read

3 Likes

Yes, it looks perfectly. :+1:

[[sns.countplot(df[column], order=df[column].value_counts().index), plt.xticks(rotation=30, fontsize=10), plt.xlabel(column, fontsize=12), plt.show(), plt.clf()] if len(df[column].unique()) > 6 else [plt.pie(df[column].value_counts(), labels=df[column].unique()), plt.axis('equal'), plt.show(), plt.clf()] for column in columns]

I was able to solve it with this code