Data Visualization using Matplotlib & Seaborn
Tags: Data Visualization, Seaborn, Data Science, Matplotlib

# Setting the data

years = [2013,2014,2015,2016,2017,2018,2019,2020,2021]

Sho = [0,0,0,0,0,0,1,1,1]

Chu_M = [0,0,0,0,1,2,2,2,2]

Chu_S = [1,2,4,7,8,18,18,20,22]

Oku_Nosyu = [0,0,0,1,1,3,4,4,4]

Oku_Shin = [0,0,0,0,3,4,8,10,10] # set bar width

width=0.15 # axis index

years_index = np.arange(len(years)) # clear reset plt style

plt.style.use('default')

plt.style.use(['ggplot'])

plt.figure(figsize=(8,5)) # plt interface approach for bar charts

plt.bar(years_index-2*width, Sho, color='#c6ddf1', label='Sho', width=width, linewidth=0.4,edgecolor='darkgrey') plt.bar(years_index-width, Chu_M, color='#99b6ce', label='Chu_M', width=width) plt.bar(years_index, Chu_S, color='#6d91ad', label='Chu_S', width=width) plt.bar(years_index+width, Oku_Nosyu, color='#416e8c', label='Oku_Nosyu', width=width) plt.bar(years_index+2*width, Oku_Shin, color='#004c6d', label='Oku_Shin', width=width) plt.legend() plt.title('Iaito model ownership',fontsize=ts)

plt.xlabel('Year',fontsize=xls)

plt.xticks(ticks=years_index, labels=years)

plt.ylabel('Count',fontsize=yls)

plt.yticks(list(np.arange(df_cumsum['count'].max()+1))) plt.show()

Image by Author

Depending on the message to convey from the data, the characteristics of the data, and the target audience, certain plots might be more suited. For example, the following catplot might convey the number of saya designs across the five models and their additional costs. But as the number of data points increases, that may not be scalable. It would be better to split the message, for example, the popularity of saya designs by quantity and distribution of saya design prices. This was what I did with a radial plot for the latter direction. The code snippets are after the catplot.

ax = sns.catplot(x="model", y="saya(cost)", data=df_saya1,

hue="saya", palette= 'tab20b', kind="swarm", s=10,

height=4.5, aspect=2.5)

ax.fig.suptitle('Saya across models',fontsize=ts)

ax.fig.subplots_adjust(left=0.1, top=0.9)

ax.set_xlabels('')

ax.set_ylabels('price (JPY)', fontsize=yls)

ax.set_xticklabels(fontsize=xts)

ax.set_yticklabels(fontsize=yts)

ax._legend.set_title('Saya') plt.show()

Image by Author | Scalability is an issue as data points increases

# replicating saya color is impractical from experience. Use a sequential color scheme

palette_saya = ['#7eedff','#6dd7ed','#5dc2dc','#4dadc9','#3e99b7','#3085a5','#217192','#125e7f','#004c6d',

'slateblue','rebeccapurple','purple','indigo'] # initialize the figure

plt.figure(figsize=(10,10))

ax = plt.subplot(111, polar=True);

plt.axis('off')

plt.title('Saya ranked by price (JPY)',y=.9,fontsize=ts) # set coordinate limits

upperlimit = 100

lowerlimit = 30 # compute max and min of dataset

max_ = df_saya2['saya(cost)'].max()

min_ = df_saya2['saya(cost)'].min() # compute heights (conversion of saya_charge into new coordinates)

# 0 will be converted to lower limit (30)

# max_ converted to upper limit (100)

slope = (max_ - lowerlimit)/max_

heights = slope * df_saya2['saya(cost)'] + lowerlimit # width of each bar

width = 2*np.pi / len(df_saya2.index) # compute angle each bar is centered on

indexes = list(range(1, len(df_saya2.index)+1))

angles = [element * width for element in indexes] # draw

bars = ax.bar(x=angles, height=heights, width=width, bottom=lowerlimit,

linewidth=1,edgecolor="white",color=palette_saya) # padding between bar and label

labelPadding = 1000 # label

for bar, angle, height, label in zip(bars,angles,heights,df_saya2['saya_']):

# specify rotation in degrees

rotation = np.rad2deg(angle)



#flip some labels upside down for readability

alignment = ""

if angle >= np.pi/2 and angle < 3*np.pi/2:

alignment = "right"

rotation += 180

else:

alignment = "left"



# add label

ax.text(x=angle, y=lowerlimit + bar.get_height() + labelPadding,

s=label, ha=alignment, va='center', rotation=rotation, rotation_mode="anchor",size=12) plt.show()

Image by Author | Unconventional but works in this case

Last but not least, xkcd styles. xkdc plots with their balance of humor, scientific jargon, and perspectives hold a special place in the minds of many. This plotting style can be called out from matplotlib via plt.xkcd() like so. The output is the first image for this article. It comes as a surprise that a simple-looking plot in xkcd’s style requires a fair amount of planning and thought, particularly annotation locations and arrow positioning.