-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: allow seaborn to annotate a subplot with 0 counts, when there are 0 counts for all bars in a subplot #3568
Comments
First, it is better to provide a reproducible example, typically using the datasets integrated to seaborn. But at least your problem is somewhat clear. Secondly, this is not entirely a seaborn issue. You are basically using a "hack" to get your desired result, as |
Thanks @thuiop, that's one good suggestion. Alternatively, if you want to have "0 height bars" you can compute the counts yourself (including for the unobserved categories) and then use I am going to close as this is a question about building on top of seaborn not a question about seaborn functionality itself. You may want to ask on StackOverflow (but you'll definitely want to reduce the example to something that others can work with). |
@thuiop thanks for your advice, will take it to heart for future posts =). Thanks for your advice on how to solve this, I am interested in seeing your solution and learning from you, so would it be possible for me to see your code please? Not sure if this is allowed since mwaskom has closed this thread, but if it is, I have provided a modified form of my dataset here I also modified my code above to make it work with this attached dataset, not sure if you want it, but it is below
|
@mwaskom thanks very much for your reply too. Yes using a barplot instead could work, so I will keep it in mind =) |
@jonng1000 my unsolicited advice is that you'd make it a lot easier for someone to help you if you reduce your example much further to eliminate any steps that aren't relevant to the specific question that you are asking. For example, all of the aesthetic tweaks you're doing (despining, modifying labels, setting non-default colors, etc.) are orthogonal to the specific question that you have, but they make it harder to digest your code and suggest where you want to make changes. Additionally, it is a good exercise for you: often the process of simplifying an example will help you understand the parts better and perhaps even give rise to a spontaneous insight about the solution. This post may be helpful (it is written about "bug reports" but most of it applies here too): https://matthewrocklin.com/minimal-bug-reports |
@jonng1000 it was trickier than I thought actually, so I had to do the counting with pandas instead of full seaborn ; this code should do the trick
I did not do all the aesthetic stuff, I leave that to you to tinker with (I agree with the above post though, a lot of it is a bit outside of scope of your real question).
|
I think that might be the issue tracked here? #2981 |
Oh, yes, seems like it. |
Hi, first up, wanna say to the developer, thanks for developing seaborn! It has been a great tool for me to visualise data =). Would like to ask if its possible to add the ability for seaborn, to annotate a subplot with 0 counts, when there are 0 counts for all bars in a subplot? A more detailed explanation is below.
I have code using seaborn
catplot
, to draw categorical plots onto a FacetGrid. I am using acountplot
in thecatplot
function, hence am usingkind='count'
. Thecol
argument in thecatplot
is set to thecol_cat
variable, which in this context is defined asage_category
.age_category
is a column in mydf
, which as its name suggests, represents age categories. This is an ordered pandas categorical dtype.My
df
is as follows:The
age_category
column indf
is as follows:The distribution of categories in the
age_category
column is as follows:The number of subjects in the age category of '>= 80 years old' is 0, which gives me problems in plotting its annotations for the bars.
In general, the code which is below works. My objective is to plot multiple subplots, one for each age category, showing the subject counts for each combination of
spirometryResult_category
andhabits-smoking
.The output picture is show here:
![image](https://private-user-images.githubusercontent.com/34784455/284836532-fb74f2e1-5c57-4b9e-81a2-b83571f6d4da.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEzMjkyNzgsIm5iZiI6MTcyMTMyODk3OCwicGF0aCI6Ii8zNDc4NDQ1NS8yODQ4MzY1MzItZmI3NGYyZTEtNWM1Ny00YjllLTgxYTItYjgzNTcxZjZkNGRhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE4VDE4NTYxOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ2NWY3N2M5ZDMzZjdlMzc2NDAyMjJmMmRiNWFiZWU5YTE1ZWZjMTdiNTVhODk4M2JjMWNmYzI3Mjc3OWZjMjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.iI_8XVbLY1VEWFQ004ThIoybsYaQXQjE962n1IL-teU)
As you can see, the category of ">= 80 years old" has no subjects, hence for its corresponding subplots, the text "0" is not plotted at all. All other age categories have their corresponding bars and annotations created correctly. For this case, where ">= 80 years old" has no subjects,
ax.containers
is an empty list, therefore my for loop usingfor container in ax.containers:
to annotate cases with 0 counts, does not work.How do I force seaborn to annotate subplots with 0 counts, in the correct location (automatically decided by seaborn so i dont have to hardcode anything), in this case, where the category has 0 subjects, and
ax.containers
is an empty list? Seaborn doesn't seem to allow me to do that, so would it be possible to add this in please?The text was updated successfully, but these errors were encountered: