overlapping python stacked bar graphs

100% stacked bar chart python
matplotlib stacked bar chart with values
grouped stacked bar chart python
stacked bar chart pandas
python stacked bar chart
stacked bar plot
stacked bar and line chart python
stacked bar chart python stack overflow

Is there a way to set the zorder of each dataset in a multiple bar graph different at each x location so that all of the information is visible.

axes.bar(position,data_1,color='g')
axes.bar(position,data_2,color='r')
axes.bar(position,data_3,color='b')

for example, if a blue value is greater than a green value the green will be hidden behind and visa versa. setting alpha to lower values than one creates more than 3 colors displayed from mixing of colors.

One way to do it is to sort the bars individually at each bar location:

import matplotlib.pyplot as plt
import numpy as np

L = 5

heights_a = 10. + np.random.randn(L)
heights_b = 10. + np.random.randn(L)
heights_c = 10. + np.random.randn(L)

position = np.arange(L)
colors = ['C0', 'C1', 'C2']

plt.figure()

for x, ha, hb, hc in zip(position, heights_a, heights_b, heights_c):
    for i, (h, c) in enumerate(sorted(zip([ha, hb, hc], colors))):
        plt.bar(x, h, color=c, zorder=-i)

plt.show()

which looks like this:

Create Overlapped/Superimposed Barchart - Dash, Hi, I need to change stacked barchart width to be overlapped like the /​23293011/how-to-plot-a-superimposed-bar-chart-using-matplotlib… In this post, we will see how we can plot a stacked bar graph using Python’s Matplotlib library. A stacked bar graph also known as a stacked bar chart is a graph that is used to break down and compare parts of a whole. Stacked Bar Graphs place each value for the segment after the previous one. The total value of the bar is all the segment

I don't have enough reputation to comment, but @cheersmate's solution needs a small fix when there are negative values in the heights (e.g., height above/below sea level). This can be easily fixed by sorting by the absolute value of the height:

for x, ha, hb, hc in zip(position, heights_a, heights_b, heights_c):
    for i, (h, c) in enumerate(sorted(zip([ha, hb, hc], colors), key=lambda (h,c): (abs(h), c))):
        plt.bar(x, h, color=c, zorder=-i)

Stacked Bar Graph, This is an example of creating a stacked bar plot with error bars using bar . Note the parameters yerr used for error bars, and bottom to stack the  This section display grouped barcharts, stacked barcharts and percent stacked barcharts. This 3 types of barplot variation have the same objective. It displays a numerical value for several entities, organised into groups and subgroups.

You're in luck! plot has a zorder kwarg.

I tested it on bar just to be sure, using an example I have laying around.

summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,
                label="Summer", zorder=2)

winter = ax.bar(index, df["Crime Type Winter"].value_counts(),
                bar_width, label="Winter", zorder=1)

Gives:

And if I reverse it:

summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,
                label="Summer", zorder=1)

winter = ax.bar(index, df["Crime Type Winter"].value_counts(),
                bar_width, label="Winter", zorder=2)

Edit: I looked into the "bar within a bar" part of this, and, as noted in the comments elsewhere, it seems you would need to manually set the zorders based on a sorting of their values. You would probably want to modify the bar width based on that calculated zorder in order to get that visual effect.

Full code that I'm using as a reference example is given below for clarity:

import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

s = "Crime Type Summer|Crime Type Winter".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)

index = np.arange(5)
bar_width = 0.35

fig, ax = plt.subplots()
summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,
                label="Summer", zorder=1)

winter = ax.bar(index, df["Crime Type Winter"].value_counts(),
                bar_width, label="Winter", zorder=2)

ax.set_xlabel('Category')
ax.set_ylabel('Incidence')
ax.set_title('Crime incidence by season, type')
ax.set_xticks(index)
ax.set_xticklabels(["ASB", "Violence", "Theft", "Public Order", "Drugs"])
ax.legend()

plt.show()

Stacked bar chart, This is an example of creating a stacked bar plot with error bars using bar . Note the parameters yerr used for error bars, and bottom to stack the  Select the data range that you want to create an overlapped chart, and then click Insert > Insert Column or Bar Chart > Clustered Chart, see screenshot: 2. After creating the clustered chart, right-click one series bar, and then choose Change Series Chart Type from the context menu, see screenshot: 3.

Stacked Bar chart : Definition and Examples, Creating stacked bar charts using Matplotlib can be difficult. Often the data you need to stack is oriented in columns, while the default Pandas  Bar chart with Plotly Express¶ Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures . With px.bar , each row of the DataFrame is represented as a rectangular mark.

Python matplotlib superimpose scatter plots, Bar charts are used to display values associated with categorical data. import matplotlib.pyplot as plt %matplotlib inline plt.style.use('ggplot') x With stacked bar charts we need to provide the parameter bottom , this informs  Download Python source code: bar_stacked.py Download Jupyter notebook: bar_stacked.ipynb Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery

Easy Stacked Charts with Matplotlib and Pandas – pstblog, This section display grouped barcharts, stacked barcharts and percent Matplotlib. #12 stacked barchart. #11 Grouped barplot. #13 percent stacked bar chart. Moving those original bars to the secondary axis means we also see a second y axis scale at the top of the graph. Make sure the minimum and the maximum of both y axes match perfectly. Then delete the one at the top. It’s ruling the orange bars but its redundant. Finally, add a better font, some action color, and a succinct title.

Comments
  • You probably need to check manually which bar is higher and run bar() twice with different zorders for one of the classes.
  • yeesh. that gets exponentially more complicated with more and more datasets to plot.
  • Not necessarily. If you iterate through the bins and sort them one by one, it shouldn't be too difficult. Usually this kind of plot is done by putting the bars next to each other, though.
  • Ohh this is sweet. Thank you.
  • sorry i meant to say that the hiding behavior is undesired. I wish to be able to see both datasets no matter which one is larger.
  • So a bar within a bar basically? As opposed to putting the bars side by side
  • i think you understand what i am trying to accomplish. yes
  • Edited to respond to that, but I don't think you'll like it... is there a reason you can't use the side-by-side bar pattern? It seems much more straightforward to implement
  • side by side gets hard to read when there are many x positions