Let’s consider the below data. We would like to create a
bubble chart using matplotlib where x-axis will be the Risk Value and y-axis
will be the Financial Value. The size of the bubble would be determined by the
Cost. Every bubble would represent a project.
Data is available here.
Python code…
import numpy as np import pandas as pd import matplotlib.pyplot as plt import random #Importing Data df = pd.read_csv(r'F:\Python\Sample Data for Bubble Chart.txt', sep='\t', low_memory=True, encoding='latin-1', error_bad_lines=False, thousands=',', header=0) group = list(df['Department'].unique()) data = [] for i in group: data.append(df[df['Department']==i]) color=[] for i in range(len(df['Department'].unique())): r = lambda: random.randint(0,255) color.append('#%02X%02X%02X' % (r(),r(),r())) # Create plot fig = plt.figure(figsize=(10,7), dpi=200) ax = fig.add_subplot(1,1,1) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.xaxis.grid(b=False, which='major', color='gray', linestyle='--', alpha=0.5) ax.yaxis.grid(b=False, which='major', color='gray', linestyle='--', alpha=0.5) for d, c, g in zip(data, color, group): x=d['Risk Value'] y=d['Financial Value'] s=d['Cost']*5 ax.scatter(x, y, alpha=0.7, c=c, edgecolors='k', s=s, label=g) for i, txt in enumerate(d['Project Name']): ax.annotate(txt, (list(x)[i], list(y)[i]), fontsize=7, horizontalalignment='center', verticalalignment='center') plt.title("Risk Value, Financial Value vs Cost Ratio") plt.xlabel("Risk Value") plt.ylabel("Financial Value") plt.legend(loc=2) plt.show()
To change the canvas color and sub-plot color use the following....
ReplyDeletefig, ax = plt.subplots(figsize=(10,7), dpi=100)
plt.rcParams['figure.facecolor'] = 'w'
ax.patch.set_facecolor("w")
.
.
.