I am a beginner in Python (using Python 3.7 in Spyder 3.3.2 and Anaconda Navigator 1.9.6). I have no problem creating seaborn violin plots, but the moment I try to Facetgrid them I run into issues. I tried using catplot.
Here is my violin plot code (it works):
# Libraries
import seaborn as sns
import pandas as pd
import os # Imports `os`
from matplotlib import pyplot as plt
os.chdir(r"XXXXXX") # Changes directory
os.listdir('.') # Lists all files and directories in current directory
## Data set
File = 'test_eventcountratios.xlsx' # Assigns Excel filename to File
df = pd.read_excel(File)
ax = sns.violinplot(x = df["Timepoint"], y = df["Macrophage Frequency"], palette = "Blues")
ax.set_xticklabels(ax.get_xticklabels(),rotation=30)
My data is long form, so all timepoints are in the first column and "Macrophage Frequency" data are in the second column. All remaining columns represent other cell types. Here is a screenshot of my data spreadsheet
Here is my catplot code (it doesn't work):
g=sns.catplot(data=df, x="Timepoint", y=df["B cell Frequency","Neutrophil Frequency","NK cell Frequency","Macrophage Frequency"],
palette = "Blues",
kind = "violin", split=True)
I get "Key Error: ('B cell Frequency', 'Neutrophil Frequency', 'NK cell Frequency', 'Macrophage Frequency')"
I don't even want to call on each column individually. I would like the code to run through each column (cell type) to gather data and put each column's data into it's own plot.
I stripped the catplot code to basics to see if that worked:
g=sns.catplot(x = df["Timepoint"], y = df["Macrophage Frequency"], palette = "Blues", data=df, kind="violin")
It works and produces a violin plot, but with this error: "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
So...
I want to make a grid of multiple violin plots (Timepoint on X axis, Cell type frequency on Y axis), where each plot takes data from each column. Why am I only successful when I limit my "y" to a single column from my dataframe?
I've Googled all of my errors, but I can't seem to make the right changes to my code. If I change one thing, then I get a new error (like "TypeError: object of type 'NonType' has no len()", "ValueError: num must be 1 <= num <= 0, not 1", etc)