in Education by
I would like to highlight bars that have values exceeded the total value. import pandas as pd m_data = { "total": [30, 25, 25, 5, 15], "current": [20, 15, 15, 10, 15], "abbrev": ["A", "B", "C", "D", "E"] } m = pd.DataFrame(m_data) import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style="whitegrid") # Initialize the matplotlib figure f, ax = plt.subplots(figsize=(6, 15)) # Plot the total crashes sns.set_color_codes("pastel") sns.barplot(x="total", y="abbrev", data=m_data, label="test1", color="b") # Plot the crashes where alcohol was involved sns.set_color_codes("muted") sns.barplot(x="current", y="abbrev", data=m_data, label="test", color="b") # Add a legend and informative axis label ax.legend(ncol=2, loc="lower right", frameon=True) ax.set(xlim=(0, 35), ylabel="", xlabel="values") sns.despine(left=True, bottom=True) for i in ax.containers: ax.bar_label(i,) My output: Current image My expected: Expected I am wondering whether is there a way to just show a highlight part with different color for that exceeded value so that its easy to see that the bar is exceeded. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You could plot the bars in 4 steps: first the "totals" then the "alcohol involved" for the bars that have excess: plot them using the value of the "alcohol involved" ones, in a special color, optionally with hatching erase part of them using the value of "total", but the color of the "alcohol involved" ones The order= keyword ensures all bars are drawn at the correct position. import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import numpy as np m = pd.DataFrame({"total": [30, 25, 25, 5, 15], "current": [20, 15, 15, 10, 15], "abbrev": ["A", "B", "C", "D", "E"]}) sns.set_theme(style="whitegrid") # Initialize the matplotlib figure f, ax = plt.subplots(figsize=(6, 15)) y_order = m["abbrev"].values # Plot the total crashes sns.set_color_codes("pastel") sns.barplot(x="total", y="abbrev", data=m, label="total", color="b", ax=ax) # Plot the crashes where alcohol was involved sns.set_color_codes("muted") sns.barplot(x="current", y="abbrev", data=m, label="alcohol involved", color="b", ax=ax) m_excessed = m[m["current"] > m["total"]] if len(m_excessed) > 0: # Plot the excess bars sns.set_color_codes("bright") sns.barplot(x="current", y="abbrev", data=m_excessed, order=y_order, hatch='//', label="excess", color="b", ax=ax) # Replot the total bars, to erase part of excess bars sns.set_color_codes("muted") sns.barplot(x="total", y="abbrev", data=m_excessed, order=y_order, color="b", ax=ax) # Add a legend and informative axis label ax.legend(ncol=1, loc="lower right", frameon=True) ax.set(xlim=(0, 35), ylabel="", xlabel="values") sns.despine(left=True, bottom=True) for i in ax.containers: ax.bar_label(i) plt.tight_layout() plt.show()

Related questions

0 votes
    Which of the following value is provided by kind keyword for barplot? (a) bar (b) kde (c) hexbin ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
0 votes
    I have an Access form with a combobox and a subform on it. The subform is in datasheet mode (the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I am looking for a way to return the interpolated coordinate value of a variable at a query variable value ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I made a function which will look up ages in a Dictionary and show the matching name: dictionary = {'george' ... it search backward. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is there a way to step between 0 and 1 by 0.1? I thought I could do it like the following, but it failed: ... , which I did not expect. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to make a function that will compare multiple variables to an integer and output a string of three ... like this possible? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want to assign a value to a TensorFlow variable in Python and I am using this: import tensorflow as tf import ... do to correct this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...