Data Warehousing and Machine Learning

29 March 2021

Plot

Filed under: Python — Vincent Rainardi @ 6:41 am
# 2x3 box + bar plots for categorical variables:
plt.figure(figsize=(18,8))
plt.subplot(2,3,1)
sns.boxplot(data=df, x="season", y="count")
plt.subplot(2,3,2)
sns.boxplot(data=df, x="month", y="count")
plt.subplot(2,3,3)
sns.boxplot(data=df, x="weekday", y="count")
plt.subplot(2,3,4)
sns.countplot(df["season"])
plt.subplot(2,3,5)
sns.countplot(df["month"])
plt.subplot(2,3,6)
sns.countplot(df["weekday"])
plt.show()
# 1x2 bar plots, one without confidence interval line:
plt.figure(figsize=(10,3))
plt.subplot(1,2,1)
sns.barplot(data=df, x="month", y="count", hue="year")
plt.subplot(1,2,2)
sns.barplot(data=df, x="weekday", y="count", hue="year", ci=None)
plt.show()
# 3x3 pair plot of continuous variables 
plt.figure(figsize=(4,4))
sns.pairplot(df[["count", "temp", "windspeed"]])
plt.show()
# View the heat map of correlations between variables
plt.figure(figsize=(12,7))
correlation_data = df.corr()
correlation_data = round((correlation_data*100),0).astype(int)
sns.heatmap(correlation_data, annot=True, fmt="d", cmap="YlGnBu")
plt.show()
# Setting box plot title, x and y axis labels, range and format
ax = sns.boxplot(data=df, width=0.5, palette="Blues")
ax.set_title('Plot Title')
ax.set_xlabel("Label for x axis");
ax.set_ylabel("Label for y axis"); 
ax.set_yticklabels(['${:,}m'.format(int(x/1000000)) for x in ax.get_yticks().tolist()]);
ax.grid(which='major', linestyle=':', linewidth='1', color='grey');
for item in ([ax.xaxis.label, ax.yaxis.label]):
    item.set_fontsize(14);
    item.set_color("blue"); #set the x and y label to blue
    
# Draw 2 red lines showing the preferred range
ax.axhline(5000000, ls='-', linewidth='1', color='red')
ax.axhline(15000000, ls='-', linewidth='1', color='red')
ax.set_ylim(0, 80000000); #set the upper limit of y axis

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: