1
votes

I am plotting different columns of a year of hourly data from a Pandas dataframe. I have written the following function to plot:

    def plotResults(dfA):
    fig, (ax1,ax2) = plt.subplots(2, sharex=True, sharey=False)

    ax1.plot(dfA.index, dfA['LP1'], color='blue',alpha=0.7, label='LP1')

    ax1.bar(dfA.index, dfA['b'], color='green',alpha=0.1, rwidth=1,  label='b') 
    ax1.bar(dfA.index, dfA['p'].fillna(0), color='red',alpha=0.1, rwidth=1, label='p', bottom=dfA['b'])
    ax1.legend(loc='best')
    ax2.plot(dfA.index, dfA['Residual'], color='red',alpha=0.7, label='LP1')
    ax2.legend(loc='best')

    plt.show()

The bars for the bar charts are overlapping for some reason that I do not understand. I have been trying with width = 1.0/(len(dfA.index)) but then the bars get extremely narrow.

How can I set up the bars so they do not overlap and cover one hour (which is the periodicity of dfA)?

an example Picture zoomed in:

There should be gaps between the red bars in the upper graph. They only have values for some hours.

1

1 Answers

0
votes

I havent got to any solution about this overlapping bars in the barchart, but I have found a Workaround that solves the problem for me.

Using a stackplot instead of barchart.

   def plotResults(dfA):
    fig, (ax1,ax2) = plt.subplots(2, sharex=True, sharey=False)
    # Upper Chart
    # Linechart
    ax1.plot(dfA.index, dfA['LP1'], color='blue',alpha=0.7, label='LP1')
    # stackplot
    ax1.stackplot(dfA.index,dfA['b'],dfA['p'], label=['b', 'p'], colors=['green','red'] )   #stackplot labels do not show in Legend
    ax1.plot([], [], color='red', label='p', linewidth=10)      #dummy plots only to show labels in the legend
    ax1.plot([], [], color='green', label='b', linewidth=10)    #dummy plots only to show labels in the legend
    ax1.legend(loc='best')
    # Lower Chart - Residuals
    ax2.plot(dfA.index, dfA['Residual'], color='red',alpha=0.7, label='Residual')
    ax2.legend(loc='best')
    plt.show()

Using the stackplot is not an answer to the initial issue but it solves the Problem for me. enter image description here