0

I have the following dataframe. I am trying to plot column D as a line on top of grouped bars formed by columns A,B,C

                          A          B          C          D
transaction-yymm                                            
2020-01           57.959309  48.548405  49.299329  52.194553
2020-02           61.553055  53.417567  54.202018  57.408854
2020-03           56.547181  58.008887  72.120066  57.890824
2020-04           55.753265  61.955893  54.729484  57.859005
2020-05           45.946629  53.556755  70.761547  50.422332
2020-06           57.187696  50.091579  61.117012  52.937711
2020-07           60.745847  49.130193  62.152087  55.802450
2020-08           57.132313  61.146969  67.256703  58.839225
2020-09           61.004986  48.943253  66.883187  55.379921
2020-10           53.730259  47.147629  67.424503  50.894193
2020-11           57.795307  48.062595  70.050770  53.761565
2020-12           56.211362  50.902046  51.183654  53.384819

ax = df[['D']].plot(kind='line', linestyle = '-',marker='o', linewidth=0.5,color='black',markersize = 0.5)
df[['A','B','C']].plot(kind='bar',ax=ax).legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
ax.axes.set(xlabel = 'Months',title = 'Testing',ylabel = '%')

However, in the plot (below), the line representing column D never show up. Moreover, the x axis ticks for 2020-02 become horizontal all of a sudden. What have I done wrong here ? How can I fix this ?

enter image description here

Data dictionary below:

{'A': {Period('2020-01', 'M'): 57.959309064723996,
  Period('2020-02', 'M'): 61.55305496637253,
  Period('2020-03', 'M'): 56.547181340755046,
  Period('2020-04', 'M'): 55.75326451214058,
  Period('2020-05', 'M'): 45.94662852619467,
  Period('2020-06', 'M'): 57.18769553850082,
  Period('2020-07', 'M'): 60.74584658494508,
  Period('2020-08', 'M'): 57.13231301454615,
  Period('2020-09', 'M'): 61.004986090099955,
  Period('2020-10', 'M'): 53.730258825001016,
  Period('2020-11', 'M'): 57.79530721652247,
  Period('2020-12', 'M'): 56.211362441437814},
 'B': {Period('2020-01', 'M'): 48.54840527416065,
  Period('2020-02', 'M'): 53.41756704641908,
  Period('2020-03', 'M'): 58.00888715461042,
  Period('2020-04', 'M'): 61.955893081634954,
  Period('2020-05', 'M'): 53.556754957615155,
  Period('2020-06', 'M'): 50.09157883512408,
  Period('2020-07', 'M'): 49.13019305779089,
  Period('2020-08', 'M'): 61.146969125846994,
  Period('2020-09', 'M'): 48.94325273632144,
  Period('2020-10', 'M'): 47.14762928978505,
  Period('2020-11', 'M'): 48.06259468494852,
  Period('2020-12', 'M'): 50.90204601317484},
 'C': {Period('2020-01', 'M'): 49.29932934755607,
  Period('2020-02', 'M'): 54.2020182747171,
  Period('2020-03', 'M'): 72.12006622062243,
  Period('2020-04', 'M'): 54.729483766577154,
  Period('2020-05', 'M'): 70.76154660374146,
  Period('2020-06', 'M'): 61.11701211598687,
  Period('2020-07', 'M'): 62.15208723948862,
  Period('2020-08', 'M'): 67.2567032581161,
  Period('2020-09', 'M'): 66.88318669497623,
  Period('2020-10', 'M'): 67.42450336557002,
  Period('2020-11', 'M'): 70.05076986345674,
  Period('2020-12', 'M'): 51.18365432772586},
 'D': {Period('2020-01', 'M'): 52.194553236973114,
  Period('2020-02', 'M'): 57.4088543173257,
  Period('2020-03', 'M'): 57.890823886006736,
  Period('2020-04', 'M'): 57.85900485404194,
  Period('2020-05', 'M'): 50.422331514015426,
  Period('2020-06', 'M'): 52.93771082107409,
  Period('2020-07', 'M'): 55.802449549532085,
  Period('2020-08', 'M'): 58.839225048021646,
  Period('2020-09', 'M'): 55.379920815947486,
  Period('2020-10', 'M'): 50.89419278496236,
  Period('2020-11', 'M'): 53.76156458158453,
  Period('2020-12', 'M'): 53.384818973220206}}
user1769197
  • 2,132
  • 5
  • 18
  • 32
  • Does this answer your question? [Pandas plot bar chart over line](https://stackoverflow.com/questions/43418820/pandas-plot-bar-chart-over-line) – Shaido Mar 03 '21 at 06:40

1 Answers1

0

I think that you made some mistakes while either defining the Period class or processing index of the dataframe. Since you have not provided us the definition of Period class, I have created one by assumption as follows:

import pandas as pd
from matplotlib import pyplot as plt 
class Period(tuple):
    def __new__(cls, month, value):
        return super().__new__(cls, month)
    def __init__(self, month, value):
        self.__month__ = month
        self.value = value
    def __repr__(self):
        return "".join(self.__month__)
    def __str__(self):
        return "".join(self.__month__)

if __name__ == '__main__':
    foo = Period('2020-02', 'M')
    print(foo.__dir__())
    print("foo", foo)
    data = {'A': {Period('2020-01', 'M'): 57.959309064723996,
        Period('2020-02', 'M'): 61.55305496637253,
        Period('2020-03', 'M'): 56.547181340755046,
        Period('2020-04', 'M'): 55.75326451214058,
        Period('2020-05', 'M'): 45.94662852619467,
        Period('2020-06', 'M'): 57.18769553850082,
        Period('2020-07', 'M'): 60.74584658494508,
        Period('2020-08', 'M'): 57.13231301454615,
        Period('2020-09', 'M'): 61.004986090099955,
        Period('2020-10', 'M'): 53.730258825001016,
        Period('2020-11', 'M'): 57.79530721652247,
        Period('2020-12', 'M'): 56.211362441437814},
        'B': {Period('2020-01', 'M'): 48.54840527416065,
        Period('2020-02', 'M'): 53.41756704641908,
        Period('2020-03', 'M'): 58.00888715461042,
        Period('2020-04', 'M'): 61.955893081634954,
        Period('2020-05', 'M'): 53.556754957615155,
        Period('2020-06', 'M'): 50.09157883512408,
        Period('2020-07', 'M'): 49.13019305779089,
        Period('2020-08', 'M'): 61.146969125846994,
        Period('2020-09', 'M'): 48.94325273632144,
        Period('2020-10', 'M'): 47.14762928978505,
        Period('2020-11', 'M'): 48.06259468494852,
        Period('2020-12', 'M'): 50.90204601317484},
        'C': {Period('2020-01', 'M'): 49.29932934755607,
        Period('2020-02', 'M'): 54.2020182747171,
        Period('2020-03', 'M'): 72.12006622062243,
        Period('2020-04', 'M'): 54.729483766577154,
        Period('2020-05', 'M'): 70.76154660374146,
        Period('2020-06', 'M'): 61.11701211598687,
        Period('2020-07', 'M'): 62.15208723948862,
        Period('2020-08', 'M'): 67.2567032581161,
        Period('2020-09', 'M'): 66.88318669497623,
        Period('2020-10', 'M'): 67.42450336557002,
        Period('2020-11', 'M'): 70.05076986345674,
        Period('2020-12', 'M'): 51.18365432772586},
        'D': {Period('2020-01', 'M'): 52.194553236973114,
        Period('2020-02', 'M'): 57.4088543173257,
        Period('2020-03', 'M'): 57.890823886006736,
        Period('2020-04', 'M'): 57.85900485404194,
        Period('2020-05', 'M'): 50.422331514015426,
        Period('2020-06', 'M'): 52.93771082107409,
        Period('2020-07', 'M'): 55.802449549532085,
        Period('2020-08', 'M'): 58.839225048021646,
        Period('2020-09', 'M'): 55.379920815947486,
        Period('2020-10', 'M'): 50.89419278496236,
        Period('2020-11', 'M'): 53.76156458158453,
        Period('2020-12', 'M'): 53.384818973220206}}
    df = pd.DataFrame(data)
    df.index = df.index.to_series().str.join("")
    ax = df[['D']].plot(kind='line', linestyle = '-',marker='o', linewidth=0.5,color='black',markersize = 0.5)
    df[['A','B','C']].plot(kind='bar',ax=ax).legend(loc='center left',bbox_to_anchor=(1.0, 0.5))
    ax.axes.set(xlabel = 'Months',title = 'Testing',ylabel = '%')
    plt.tight_layout()
    plt.show()

Result: Result

abysslover
  • 683
  • 5
  • 14