Skip to content

Pandas custom color plotting not working for multiple values #17516

Closed
@jolespin

Description

@jolespin

I am trying to do a barplot where the positive values are a certain color and the negative values are another color. I have scripts that I've used in previous versions where this was working but the current version isn't working. I am basing it off of a code snippet from: https://stackoverflow.com/questions/22311139/matplotlib-bar-chart-choose-color-if-value-is-positive-vs-value-is-negative

Is there something in the backend of the plotting wrapper that is stoping multiple color values from working?

# Versions
print("Python Version:", sys.version)
print()
print("Pandas Version:", pd.__version__)

# Input Data
data = {'M00001': 0.099999999999999978, 'M00003': 0.125, 'M00004': -0.14200000000000007, 'M00005': -1.0, 'M00007': -0.25, 'M00017': 0.14300000000000002, 'M00021': -1.0, 'M00023': -0.25, 'M00034': 0.097000000000000003, 'M00035': -0.25, 'M00048': -0.122, 'M00050': -0.25, 'M00053': -0.125, 'M00060': 0.11099999999999999, 'M00063': -0.25, 'M00115': 0.59999999999999998, 'M00116': -0.11099999999999999, 'M00126': -0.14300000000000002, 'M00127': -0.39999999999999997, 'M00144': -0.15400000000000003, 'M00149': 0.25, 'M00157': -0.125, 'M00173': -0.090000000000000024, 'M00178': -0.17300000000000004, 'M00179': -0.10399999999999998, 'M00188': 0.33300000000000002, 'M00240': -0.33299999999999996, 'M00242': -0.33299999999999996, 'M00256': 0.5, 'M00335': -0.125, 'M00359': -0.050000000000000044, 'M00360': -0.05600000000000005, 'M00377': 0.14299999999999996, 'M00459': 0.5, 'M00481': 0.5, 'M00504': 0.5, 'M00527': 0.1429999999999999, 'M00530': -0.5, 'M00532': 0.10000000000000001, 'M00549': 0.66700000000000004, 'M00609': 0.16699999999999998, 'M00614': -0.083000000000000018, 'M00618': 0.11200000000000004, 'M00628': -0.25, 'M00695': -0.33300000000000002, 'M00723': -0.25, 'M00728': 0.33300000000000002, 'M00754': 0.33300000000000002, 'M00793': -0.25, 'M90000': -0.11400000000000005, 'M91000': -0.070000000000000007}
Se_data =  pd.Series(data)

# Pandas Plotting
with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(5,10))
    Se_data.plot(kind="barh", ax=ax, color=Se_data.map(lambda x: {True:"blue", False:"red"}[x > 0]))

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions