I compared the drawing of multi-series bar graphs with matplotlib and seaborn.
In conclusion, ** seaborn is useful **
--Environment
The left side is matplotlib and the right side is seaborn. It's the same as looking at the graphs, but seaborn is easy until you draw these. The color of the stick is the same, but seaborn is a little lighter. Maybe there is a setting somewhere like this ...
This is the flow until the upper bar graph is drawn.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from io import StringIO
import numpy as np
%matplotlib inline
data = ('Number of people,sex,age\n'
'58,male,18-year-old\n'
'25,male,19 years old\n'
'42,male,20-year-old\n'
'60,Female,18-year-old\n'
'42,Female,19 years old\n'
'70,Female,20-year-old\n'
)
df = pd.read_csv(StringIO(data), dtype={'Number of people':'int32'})
print(df)
↓
Number of people | sex | age |
---|---|---|
58 | male | 18-year-old |
25 | male | 19 years old |
42 | male | 20-year-old |
60 | Female | 18-year-old |
42 | Female | 19 years old |
70 | Female | 20-year-old |
plt.rcParams['font.family'] = 'Yu Gothic' #Avoid garbled Japanese characters with Yu Gothic by default
plt.rcParams['font.size'] = 20 #Set default font size
fig,ax = plt.subplots(1, 2, figsize=(24,10)) #Create a drawing space of 24 inches wide and 10 inches high with 1 row and 2 columns
#Bar graph drawing with matplotlib divides df data a little
labels = list(df['age'].unique()) #List the locations corresponding to the X-axis labels from df
number_male = list(df['Number of people'].loc[0:2]) #dfの上から3行のNumber of peopleの数値(男性の数値)をリスト化
number_female = list(df['Number of people'].loc[3:5]) #dfの下から3行のNumber of peopleの数値(女性の数値)をリスト化
left = np.arange(len(number_male)) #For specifying the coordinates to paste the X-axis label
print(left) #The contents of left[0 1 2]
width = 0.4 #In the case of a multi-series graph, if the coordinates of the X-axis label are only left, it will shift, so the correction amount
#Bar chart with matplotlib
ax[0].bar(x=left, height=number_male, width=width, align='center', color='royalblue') #Added bar graph for male part
ax[0].bar(x=left+width, height=number_female, width=width, align='center', color='tomato') #Added bar graph for female part
ax[0].set_xticks(left + width / 2) #Specify the axis position of the 18-year-old, 19-year-old, and 20-year-old parts
ax[0].set_xticklabels(labels=labels) #"18 years old, 19 years old, 20 years old"Specified to draw
ax[0].set_xlabel('age') #X-axis label
ax[0].set_ylabel('Number of people') #Y-axis label
ax[0].legend(list(df['sex'].unique()), title='sex', loc='upper right') #凡例の男性、女性、タイトルをsex、位置を右上へ設定
ax[0].set_title('Bar chart with matplotlib', size=30) #Set title
#Bar chart with seaborn
sns.barplot(data=df, x='age', y='Number of people', hue='sex', ax=ax[1], palette={'male':'royalblue','Female':'tomato'}) #data=Specify df, set X and Y, hue='sex'とすることでsex別で分けてくれる
ax[1].legend(loc='upper right', title='sex') #Set the legend title and position to the upper right
ax[1].set_title('Bar chart with seaborn', size=30) #Set title
plt.savefig('I tried to draw a graph.png', bbox_inches='tight', pad_inches=0.3) #Save the drawn image
plt.show() #drawing
This will draw the Bar Graph here (# graph to draw).
Number of lines | |
---|---|
matplotlib | 8 |
seaborn | 3 |
Seaborn is easier to draw because you can see it! The code has two lines, a legend and a title, so you can draw multiple series of bar charts with just one line.
Recommended Posts