Seaborn keeps flipping my rows for multi-graphs - python

I'm using Seaborn for some data exploration and am using the catplot functions. When I use col='something' to create multiple graphs, I am running into an issue where the variable names of one plot do not match up with another. The colors are the same. I've tried using the order=list, but nothing seems to work.
An example would be
sns.catplot(x=variable, y=other_variable, data=df, col=something, kind='bar')
and I get something like this.

Related

Error when plotting line graph using seaborn: If using all scalar values, you must pass an index

I am trying to make a more aesthetically pleasing graph for a project and was told that seaborn would make beautiful plots but I am having trouble with it as it returns the error: If using all scalar values, you must pass an index. I'm not sure why there is this error as I am able to plot a regular graph using the same dataframe.
This is the dataframe that I am using:
and I have successfully created a graph:
ax = data1.plot(xlabel='Year', ylabel='Electricity generation capacity', figsize=(15,10), marker='.')
ax.legend(title='Electricity generation capacity by Year', bbox_to_anchor=(1, 1.02), loc='upper left')
However, the graph is quite ugly as you can barely see the trend of the bottom three lines. (I do not know if seaborn will help with this issue as I am rather new to python and am unfamiliar with data visualization using python.)
Perhaps my code is wrong but when I try to make a graph, sns.lineplot(data1) , it returns an error as mentioned above.
Please let me know how I can solve this issue (Or if I can create a better-looking graph without seaborn, please teach me). Thank you.
From your screenshot it seems like the Year is the dataframe index. Try this:
sns.lineplot (data=data1, x=data1.index)

Matplotlib - plot lines in more colors than 10

I'm trying to plot 13 different columns from a dataframe in one line graph using matplotlib and I am really struggling to understand how matplotlib works with colors. After 10 unique colors the pallet begins to loop.
There seem to be countless examples of how to do this on SO but, my limited understanding of the tool makes rhetorical examples such as these quiet inaccessible. What if my data is not an arbitrary collection of lines?
Better yet, is there anyway I can permanently change my matplotlib configuration to infinitely create new colors as needed?
I've been struggling with this for a while and can use whatever advice you can offer.
If you are working with Pandas dataframe, you can pass a cmap to plot:
df.plot(cmap='tab20')
Update: With matplotlib:
cmap=plt.get_cmap('tab20')
for i,col in enumerate(df.columns):
plt.plot(df[col], color=cmap(i), label=col)
plt.legend()

difference between countplot and catplot

In python seaborn, What is the difference between countplot and catplot?
Eg:
sns.catplot(x='class', y='survived', hue='sex', kind='bar', data=titanic);
sns.countplot(y='deck', hue='class', data=titanic);
seaborn.countplot
Shows the counts of observations in each categorical bin using bars.
seaborn.catplot
Provides access to several axes-level functions that show the relationship between a numerical and one or more categorical variables using one of several visual representations.
There is a lot of overhead in catplot, or for that matter in FacetGrid, that will ensure that the categories are synchronized along the grid. Consider e.g. that you have a variable you plot along the columns of the grid for which not every age group occurs. You would still need to show that non-occuring age group and hold on to its color. Hence, two countplots next to each other do not necessarily make up one catplot.
However, if you are only interested in a single countplot, a catplot is clearly overkill. On the other hand, even a single countplot is overkill compared to a barplot of the counts.

How to plot two different graphs on a single figure in pandas?

I am trying to get two different plots as one plot. I will not write down my entire code (is so long), but based on the two small codes below, i get two different time series and I want to put these together in one figure.
My code for the first plot:
plt.figure(figsize=(15,4))
i = plt.plot(july/july.mean(),label='G')
my code for my second plot:
spi3 = pd.read_csv('SPI3.csv',header=0,parse_dates=True)
spi3.plot(y='spi',figsize=(16,4))
Quick dirty fix would be to plot dictionaries at first, only then plot with plt.plot. Also, if you want to plot in the same figure, define figsize only in the first figure you are plotting. (Therefore plt.figure is ommitted completely.)
spi3.plot(y='spi',figsize=(16,4))
plt.plot(july/july.mean(),label='G')

How to plot independent graphs next to each other using seaborn?

my problem is that I could only find answers for plots sharing the same y-axis units.
My graphs are defined as follows:
#Plot1
sns.set_style("white")
sns.catplot(y="Reaction_cd_positive", x="Flux_cd_positive",
kind="bar",height=4, data=CDP,aspect=1.5)
#Plot2
sns.catplot(y="Reaction_cd_negative",x="Flux_cd_negative",
kind="bar",height=4, data=CDN, aspect=1.5)
Thank you in advance!
Ok, let me translate this. You are using seaborn in a jupyter notebook. You want 2 barplots next to each other within the same figure, instead of two individual figures. Since catplot produces a figure by itself, there are two options.
Create a single catplot with two subplots. To this end you would need to concatenate your two DataFrames into a single one, then use the col argument to split the data into the two subplots.
Create a subplot grid with matplotlib first, then plot a barplot into each of the subplots. This is shown in this question.

Categories