Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn countplot does not accept both x and y arguments in Python

I want to plot the frequencies of a variable y by a variable x and to that effect I am using the seaborn.countplot() method. However I am getting an error message.

For a reproducible example see below:

surveys_species_by_plot_sample

    plot_id taxa
0   1   Bird
1   1   Rabbit
2   1   Rodent
3   2   Bird
4   2   Rabbit
5   2   Reptile
6   2   Rodent
7   3   Bird
8   3   Rabbit
9   3   Rodent

The intent now is to plot the number of taxa (Birds, Rabbits etc) by plot_id. I am using the following command:

sn.countplot(x = "plot_id", y = "taxa", data = surveys_species_by_plot_sample,
             palette = sn.color_palette(palette = ["SteelBlue" , "Salmon"], n_colors = 4))

I am getting the following error message:

TypeError: Cannot pass values for both `x` and `y`

I do not understand, since the documentation states that both x and y variables can be passed to the function:

Parameters: x, y, hue : names of variables in data or vector data, optional Inputs for plotting long-form data. See examples for interpretation.

data : DataFrame, array, or list of arrays, optional Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

Your advice will be appreciated.

like image 208
rf7 Avatar asked Oct 18 '25 02:10

rf7


2 Answers

Reading examples in the Documentation, you can only pass x or y, not both. Stating X or Y changes the orientation of your chart. You might need a different chart type.

like image 179
CodeCupboard Avatar answered Oct 19 '25 18:10

CodeCupboard


If you want to use both the x and y in your plot, you can't use countplot, instead you should use sn.barplot :)

sn.barplot(x = "plot_id", y = "taxa", data = surveys_species_by_plot_sample, palette = sn.color_palette(palette = ["SteelBlue" , "Salmon"], n_colors = 4))

like image 26
tensai Avatar answered Oct 19 '25 16:10

tensai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!