twinx and sns.barplot seaborn are overlapping bars

2024/10/9 18:18:35

I would like to use sns.seaborn to display the np.sum and the np.mean on 2 different axes (with ax2 = ax1.twinx() I assume). The probem I have is that the graphs are overlapped and not readable.

Am I approaching the problem correctly? What can I do to get those bars next to each other?

import seaborn as sns
tips = sns.load_dataset("tips")
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(x="day", y="total_bill", data=tips, estimator=np.mean, ax=ax1)
sns.barplot(x="day", y="total_bill", data=tips, estimator=np.sum, ax=ax2, ci=None)

Thanks for your help

Larry

Answer

You might be better off aggregating your data with pandas and then using the standard matplotlib ax.bar() function to plot the resulting dataframe.

If you insist on using seaborn, the following is somewhat of a "hackish" way of obtaining the desired result.

To move each bars slightly to the left or to the right, I create a dummy categorical column that I'm using for hue-nesting, and I use the hue_order= parameter to request one of the plot to be on the left, and the reverse order for the second bar-plot to be on the right.

# create a dummy categorical column with only one category
invoicedb.loc[:,'dummy'] = 'dummy'f, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(x="InvoiceMonth", y="TotalInvoice", hue='dummy', data=invoicedb, estimator = np.mean, ax = ax1, color = 'r', hue_order=['dummy','other'])
sns.barplot(x="InvoiceMonth", y="TotalInvoice", hue='dummy', data=invoicedb, estimator = np.sum, ci = None, ax = ax2, color = 'b', hue_order=['other','dummy'])
# hue-nesting automatically creates a legend that we need to remove by hand
ax1.legend_.remove()
ax2.legend_.remove()
https://en.xdnf.cn/q/118549.html

Related Q&A

Cant get tensorflow on Anaconda [duplicate]

This question already has an answer here:TensorFlow Importing error ( Using Anaconda)(1 answer)Closed 2 years ago.I need tensorflow to run a guided project I did on Coursera, but I am not able to insta…

Extract number between text and | with RegEx Python

I want to extract the information between CVE and |, but only the first time that CVE appear in the txt. I have now the follow code:import re f = open (/Users/anna/PycharmProjects/extractData/DiarioOfi…

How to reset a loop that iterates over a set?

How can I reset a loop that iterates over a set? A common answer for iterating over a list is to reset the index you are using to access the list, however sets do not support indices. The point is to …

Can i set a threading timer with clock time to sync with cron job in python

I have a cron job that runs at 12, 12:30,1, 1:30. So every half hour intervals on the clock. I want to run a thread in my python code whenever the cron job runs.I have seen examples where to run a tim…

How do I make a simple countdown time in tkinter?

I am making a simple countdown timer in minutes. I cant seem to display the countdown in text label. Can someone help me?import tkinter as tk import timedef countdown(t):while t:mins, secs = divmod(t,…

Embed one pdf into another pdf using PyMuPDF

In need of help from learned people on this forum. I just want to embed one pdf file to another pdf file. So that when I go to the attachment section of the second file I can get to see and open the fi…

How to fix - TypeError: write() argument must be str, not None

Here is my code - sentence = input("Enter a sentence without punctuation") sentence = sentence.lower() words = sentence.split() pos = [words.index(s)+1 for s in words] hi = print("This s…

Is there a way to get source of a python file while executing within the python file?

Assuming you have a python file like so#python #comment x = raw_input() exec(x)How could you get the source of the entire file, including the comments with exec?

How can I stop find_next_sibling() once I reach a certain tag?

I am scraping athletic.net, a website that stores track and field times. So far I have printed event titles and times, but my output contains all times from that season rather than only times for that …

How can I make a map editor?

I am making a map editor. For 3 tiles I have to make 3 classes: class cloud:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.im…