pythonmatplotlibplotbar-chartlinestyle

How to make a barplot not based of labels?


I don't have a dummy code so I will try to explain it as good as I can. If no success I will try to think of a code to make it more clear.

My question is regarding making a barplot. I'd like to make a plot that instead of having the traditional solid line that goes from point to point, would instead have bars starting from the min value of the plot (let's say 0) to the value at that index. So for every tick on the x axis there is a bar trace.

All barplot examples that I have seen are using labels as x axis (i.e. Plot a bar using matplotlib using a dictionary) but in my case I don't want my x axis to be a label but rather the index of a dataframe.

I have tried to implement the barplot but I usually get an error. The first pic is what I have and it is a more classic type of plot. The second pic is what I'd like to have with the green traces (added manually) being the type of linestyle that I want. I couldn't find anything online but it seemed weird that no one does this type of plots... figure 1

figure 2


Solution

  • Your question is unclear, do you just want to use plot.bar?

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'value': [0, 0, 0, 1, 3, 5, 4, 6, 3, 3, 0, 1, 4, 3, 0, 0]})
    
    ax = plt.subplot()
    df.plot(ax=ax)      # line plot
    df.plot.bar(ax=ax)  #  bar plot
    

    Output:

    enter image description here