1

I want to plot two bar graphs in a single figure as you can see nrows=1, ncols=2, I have created 2 columns but unable to utilize it, however, the code works fine the only problem is, it prints two bar graphs separately and I want them to be printed in a single graph. Looking forward to a friend in need is a friend indeed who can guide me. Thanks

Required_File

Desired_Output

Current Program Output

import pandas as pandas
import csv
import numpy as np
import matplotlib.pyplot as plt

width=0.2

fig,ax= plt.subplots(nrows=1, ncols=2, figsize=(14, 5), dpi=100)
colors = ['red', 'yellow', 'blue', 'green']
labels = ('Cyber incident', 'Theft of paperwork or data storagedevice', 'Rogue employee', 'Social engineering / impersonation')
identify=['Health service providers','Finance','Education','Legal,accounting & management services','Personal services']

df = pd.read_csv('Malicious_or_criminal_attacks_breakdown-Top_five_industry_sectors_July-Dec-2019.csv', index_col=0, engine='python') # opening the file
df = pd.DataFrame(df)


data = df.values.tolist()

# Set position of bar on X axis
# -----------------------------------

br = [np.arange(len(data[0]))]
for i in range(len(data)):
    br.append([x + width for x in br[i-1]])
    br.append([x + width for x in br[i-1]])

for i in range(len(br)):
    if(i==4):
        break
    plt.bar(br[i], data[i], color =colors[i], width = width,
        edgecolor ='grey', label =labels[i])
    # Adding Xticks
    plt.ylabel('Number Of Attacks', fontweight ='bold', fontsize = 15)
    plt.title('Type of attack by top five industry sectors')
    plt.xticks([r + width for r in range(len(data[i]))],
            identify[:],rotation=89.5)


plt.legend()  
plt.show()
# -----------------------------------------------
arr = np.array(data)
n_groups, N = arr.shape
ind=np.arange(N)
p=[]
for i in range(len(data)):
    p.append(plt.bar(ind, data[i], width))
# for 
plt.ylabel('Number Of Attacks', fontweight ='bold', fontsize = 15)
plt.title('Type of attack by top five industry sectors')
plt.xticks(ind+width/2, identify[:],rotation=89.5)
plt.yticks(np.arange(0, 80, 20))
for i in range (len(p)-1):
    plt.legend(labels[:])
 

plt.show()

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.