Edit the width of bars using pd.DataFrame.plot()

For anyone coming across this question: Since pandas 0.14, plotting with bars has a ‘width’ command: https://github.com/pydata/pandas/pull/6644 The example above can now be solved simply by using df.plot(kind=’bar’, stacked=True, width=1) See pandas.DataFrame.plot.bar or pandas.DataFrame.plot with kind=’bar’. When changing the width of the bars, it might also be appropriate to change the figure size by specifying … Read more

How to prevent x-axis labels from overlapping

I think you’re confused on a few points about how matplotlib handles dates. You’re not actually plotting dates, at the moment. You’re plotting things on the x-axis with [0,1,2,…] and then manually labeling every point with a string representation of the date. Matplotlib will automatically position ticks. However, you’re over-riding matplotlib’s tick positioning functionality (Using … Read more

stacked bar plot using matplotlib

You need the bottom of each dataset to be the sum of all the datasets that came before. you may also need to convert the datasets to numpy arrays to add them together. p1 = plt.bar(ind, dataset[1], width, color=”r”) p2 = plt.bar(ind, dataset[2], width, bottom=dataset[1], color=”b”) p3 = plt.bar(ind, dataset[3], width, bottom=np.array(dataset[1])+np.array(dataset[2]), color=”g”) p4 = … Read more

Plot multiple columns of pandas DataFrame on the bar chart

Tested in python 3.11, pandas 1.5.1, matplotlib 3.6.2 Sample Data and Imports import pandas as pd import matplotlib.pyplot as plt import numpy as np np.random.seed(2022) # creates a consistent sample y = np.random.rand(10,4) y[:,0]= np.arange(10) df = pd.DataFrame(y, columns=[“X”, “A”, “B”, “C”]) X A B C 0 0.0 0.499058 0.113384 0.049974 1 1.0 0.486988 0.897657 … Read more

How to have clusters of stacked bars

I eventually found a trick (edit: see below for using seaborn and longform dataframe): Solution with pandas and matplotlib Here it is with a more complete example : import pandas as pd import matplotlib.cm as cm import numpy as np import matplotlib.pyplot as plt def plot_clustered_stacked(dfall, labels=None, title=”multiple stacked bar plot”, H=”/”, **kwargs): “””Given a … Read more