Plot data using facet-grid in seaborn [duplicate] - pandas

This question already has answers here:
How to change the number or rows and columns in my catplot
(2 answers)
Seaborn multiple barplots
(2 answers)
subplotting with catplot
(1 answer)
Closed 4 months ago.
I have this dataset table
And i want to plot profit made by different sub_category in different region.
now i am using this code to make a plot using seaborn
sns.barplot(data=sub_category_profit,x="sub_category",y="profit",hue="region")
I am getting a extreamly huge plot like this output
is there is any way i can get sub-plots of this like a facet-gird. Like subplots of different sub_category. I have used the facet grid function but it is the also not working properly.
g=sns.FacetGrid(data=sub_category_profit,col="sub_category")
g.map(sns.barplot(data=sub_category_profit,x="region",y="profit"))
I am getting the following output
As you can see in the facet grid output the plots are very small and the bar graph is just present on one grid.

See docs on seaborn.FacetGrid, particularly the posted example, where you should not pass the data again in the map call but simply the plot function and x and y variables to draw plots to corresponding facets.
Also, consider the col_wrap argument since you do not specify row to avoid the very wide plot output.
g=sns.FacetGrid(data=sub_category_profit, col="sub_category", col_wrap=4)
g.map_dataframe(sns.barplot, x="region", y="profit")

Related

How to show probability density for a KDE plot in Seaborn? [duplicate]

This question already has answers here:
Seaborn kde plot plotting probabilities instead of density (histplot without bars)
(1 answer)
Seaborn distplot y-axis normalisation wrong ticklabels
(1 answer)
How to normalize seaborn distplot?
(2 answers)
Why does the y-axis of my distplot contain values up to 150?
(1 answer)
Closed 17 hours ago.
I'd like to construct a univariate KDE plot using Seaborn. The x-axis is amygdala volume, and I would like the y-axis to display probability densities. But when I use seaborn's kdeplot method, it seems like it's returning the raw counts (as if it was a histogram) instead of the densities. How do I get it to show the densities?
I've tried looking for parameters to include, but nothing would help.
plt.figure(figsize=(15,8))
sns.kdeplot(data=n90pol, x='amygdala', bw_adjust=0.5)
plt.xlabel('Amygdala Volume', fontsize=16);
plt.ylabel('Density', fontsize=16);
plt.title('KDE plot of Amygdala Volume', fontsize=24)

Cannot plot a histogram from a Pandas dataframe

I've used pandas.read_csv to generate a 1000-row dataframe with 32 columns. I'm looking to plot a histogram or bar chart (depending on data type) of each column. For columns of type 'int64', I've tried doing matplotlib.pyplot.hist(df['column']) and df.hist(column='column'), as well as calling matplotlib.pyplot.hist on df['column'].values and df['column'].to_numpy(). Weirdly, nthey all take areally long time (>30s) and when I've allowed them to complet, I get unit-height bars in multiple colors, as if there's some sort of implicit grouping and they're all being separated into different groups. Any ideas about what I can do to get a normal histogram? Unfortunately I closed the charts so I can't show you an example right now.
Edit - this seems to be a much bigger problem with Int columns, and casting them to float fixes the problem.
Follow these two steps:
import the Histogram class from the Matplotlib library
use the "plot" method, which will accept a dataframe as argument
import matplotlib.pyplot as plt
plt.hist(df['column'], color='blue', edgecolor='black', bins=int(45/1))
Here's the source.

How to group a Box plot by the column names of a data frame in Seaborn? [duplicate]

This question already has answers here:
Boxplot of Multiple Columns of a Pandas Dataframe on the Same Figure (seaborn)
(4 answers)
Closed 24 days ago.
I'm a beginner trying to learn data science and this is my first time using the seaborn and matplotlib libraries. I have this practice dataset and data frame :
that I want to turn into a box plot and I want the x-axis to have all of the column names and the y-axis to range from 0 - 700 but, I'm not sure what to do.
I tried using : random_variable = sms.boxplot(data = df, x = ?, y = 'TAX')
which does give me the y-axis that is close to what I am looking for but, I don't know what the x-axis should be set equal too.
I thought may I could use the keys of the dataframe but, all I got was this mess that doesn't work:
random_variable = sms.boxplot(x = df.keys(), y = df['TAX'])
I want it to look like this but, I'm really lost on how to do this:
I apologize if this is an easy fix but, I would appreciate any help.
If you just want to display your data like that go with
import seaborn as sns
sns.boxplot(data=df)

problem in reordering the graph axis ggplot2, phyloseq

i have a shiny appp created which plots metagenome data using ggplot2, phyloseq and plotly with dplyr and tidyr. It creates pretty good stacked barplots and heatmaps only problem is it reorders sample names at x-axis e.g. 1-10 are arranged as 1,10,2,,5,6... how to correct that bug?

How to concatenate two `Facetgrid` plots? [duplicate]

This question already has answers here:
How to plot multiple Seaborn Jointplot in Subplot
(4 answers)
Closed 4 years ago.
Using seaborn, I created two Facetgrid (seaborn.axisgrid.FacetGrid) plots as follows:
#load dataset
import pandas as pd
iris=pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
#plotting
import matplotlib.pyplot as plt
import seaborn as sns
g1 = sns.FacetGrid(pd.melt(iris.loc[:,['sepal_length','sepal_width','species']], id_vars='species'), col='variable')
g1.map(sns.boxplot, 'species','value',order=['setosa','versicolor','virginia'])
g2 = sns.FacetGrid(iris, col="species")
g2.map(plt.scatter, "sepal_length", "sepal_width")
Two separate plots were generated:
and
I would like to have them in a single figure (and save as a single pdf file). However, I could not find any way to concatenate the plots (through python). I can always use Illustrator/Inkscape or bash commands to concatenate separate plots. However I want to avoid that and do all the steps in python alone.
I tried fig.axes.append (fig.axes.append(g1.axes.flatten()[0])), but the subplots ended up overlapping each other.
Question: Is it possible to concatenate two seaborn-Facegrid plots?
Personally when I need to have multiple figures displayed together I will have an html output with embedded references to each of the plots I want to display. That way you can review the analysis all in one place on a browser.