1

I need to get two line charts & bar chart in same diagram. Both line charts have the same y axis, but the bar chart has a different axis.

Table format

Indicator Name                                     2011 2012 2013 2014 2015 2016 2017 2018 2019
Bank nonperforming loans to total gross loans (%) 3.8   3.6  5.5  4.2  3.2  2.6  2.4  3.4  4.2
Bank nonperforming loans to total net loans (%)   3     2.2  3.8  2.6  1.2  1.7  1.3  2.2  2.5 
Bank Total gross loans ( LK Bn)                   99   116.6 191  165 152.8 142.3 160.7 263.1 275.5 

This is my code:

df.loc['Bank nonperforming loans to total gross loans (%)', years].plot(kind = 'line',color='mediumvioletred',marker ='o',
markerfacecolor ='blue',markersize=9,label = "NPL %")
df.loc['Bank nonperforming loans to total net loans (%)', years].plot(kind = 'line',color='blue',label = "SL")
plt.twinx()       
df.loc['Bank Total gross loans (LK Bn)', years].plot(kind = 'Bar',color='brown',label = "chk")
plt.ylim([90,280])
plt.title('Immigration from Afghanistan')
plt.ylabel('NPL %')
plt.xlabel('years')
plt.legend()

Below is the graph that I get, but it doesn't show the bar graph.

enter image description here

JohanC
  • 71,591
  • 8
  • 33
  • 66
lousia
  • 29
  • 5
  • Since you seem to be new to Stack Overflow, you should read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Sheldore Feb 10 '20 at 18:33
  • Looks like the second axes is not defined at all. I would recommend the use of two separate axes, `ax1` and `ax2` as in here: https://matplotlib.org/gallery/subplots_axes_and_figures/two_scales.html#sphx-glr-gallery-subplots-axes-and-figures-two-scales-py – SKPS Feb 10 '20 at 23:07

1 Answers1

1

Your case requires more control than DataFrame.plot may provide. You need to define order of your plots: lines should on the top of bar plot, but in addtion you bar is on twinned axis, this creates another problem. Here is solution for your problem mainly based on this answer.

Code:

import pandas as pd
import matplotlib.pyplot as plt


data = {
    2011: [3.8, 3, 99],
    2012: [3.6, 2.2, 116.6],
    2013: [5.5, 3.8, 191],
    2014: [4.2, 2.6, 165],
    2015: [3.2, 1.2, 152.8],
    2016: [2.6, 1.7, 142.3],
    2017: [2.4, 1.3, 160.7],
    2018: [3.4, 2.2, 263.1],
    2019: [4.2, 2.5, 275.5],
}

df = pd.DataFrame(
    data,
    index=['Bank nonperforming loans to total gross loans (%)',
           'Bank nonperforming loans to total net loans (%)',
           'Bank Total gross loans (LK Bn)'],
    columns=data.keys()
)

years = list(data.keys())

fig, ax = plt.subplots()

# axis for the bar
ax2 = ax.twinx()
ax2.set_ylim([90, 280])

# creating a cloned axis from ax for lines
# wee need this to put line plots on bars
ax1 = fig.add_axes(ax.get_position(), sharex=ax, sharey=ax)
ax1.set_facecolor('none')
ax1.set_axis_off()
ax1.set_xticks(years)

bar = ax2.bar(
    years,
    df.loc['Bank Total gross loans (LK Bn)'], color='brown',
    label='chk',
)

line1, = ax1.plot(
    df.loc['Bank nonperforming loans to total gross loans (%)', years],
    color='mediumvioletred',
    marker='o',
    markerfacecolor='blue',
    markersize=9,
    label='NPL %',
)

line2, = ax1.plot(
    df.loc['Bank nonperforming loans to total net loans (%)', years],
    color='blue',
    label='SL',
)

ax.set_title('Immigration from Afghanistan')
ax.set_ylabel('NPL %')
ax.set_xlabel('years')

ax2.legend([bar, line1, line2], ['chk', 'NPL %', 'SL'])

Plot:

image

trsvchn
  • 8,033
  • 3
  • 23
  • 30