Python grouping and drawing box plots

Python box plot drawing is very simple, and there are many ways to achieve, you can read this article, a variety of python box plot drawing methods . But the strange thing is that there is no way to group and draw box plots on the Internet. The so-called grouped box chart is to first perform a groupby operation on the data, and then draw a box chart for each group. For R, just use ggplot2, and there are many tutorials, but it seems that there is no python-based. In fact, the boxplot method that comes with the basic plt and dataframe really cannot be drawn in groups. To achieve this function, you need to use the seaborn package. of. Draw box diagram portals in groups . This link is about seaborn's boxplot method. Since it is written in Chinese, it is clearly written, so I won’t explain it too much. Let's post a code for grouping box plots I wrote below.

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
# plt.rcParams['savefig.dpi'] = 300 #图片像素
# plt.rcParams['figure.dpi'] = 300 #分辨率
# plt.figure(figsize=(50, 5))
sns.boxplot(x='groupfield', y='data', hue='groupfield', data=tmp , color = 'g' , linewidth = 0.5 , fliersize = 1)
sns.despine(offset=10, trim=True)
plt.xticks(rotation=90)
# plt.legend(markerscale = 5 , bbox_to_anchor=(1.4,0.8))
plt.show()

The tmp inside is a dataframe with two columns. The format is similar to the following. The above code realizes the function of using groupfield as the grouping basis, and then using data to draw a box chart after grouping, which is very simple.

groupfield data
A $1600
A $12
B $1

Guess you like

Origin blog.csdn.net/qq_39805362/article/details/105379523