Wechat official account: “Python reading money” if you have any questions or suggestions, please leave a message

Matplotlib advanced chart drawing 1, hand in hand to teach you how to draw a nice graph from 0 to 1.

The data used in this paper are shown in the figure. The Dataframe shows the region and salary of the relevant position, in thousands. The salary status of each city should be calculated.

The end goal is to use Matplotlib in conjunction with Seaborn to achieve this visualization

First, import the package you want to use, and do some font Settings because you need to display Chinese text in the diagram.

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# set Chinese font to Microsoft Yahei
plt.rcParams['font.sans-serif'] = 'SimHei'
Copy the code

Use Seaborn to generate a basic bar chart and title the chart, then make further changes around the chart.

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax)
ax.set_title('Comparison of Salary levels by city')
Copy the code

It is obvious that the font of the scale label in horizontal and vertical coordinates is a little small, and the scale line is not nice to display, so the first step is to enlarge the font of the scale label and remove the scale line.

Because calibration is a tick property, ax.tick_param() is used to set the calibration label, labelsize is used to specify the calibration labelsize, and length is used to set the calibration line length.

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax)
ax.set_title('Comparison of Salary levels by city')

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)
Copy the code

The second step is to get rid of the border (really ugly). There are two ways to do this.

The first is to set top, bottoom, left, and right using ax.spines[‘xx’].set_visible(False) as described in the previous article.

The second way, since there is only one Axes and all four borders are removed, you can also use plt.box(False).

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax)
ax.set_title('Comparison of Salary levels by city')

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)

Method of # 1:
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)

Method of # 2
plt.box(False)
Copy the code

Next, to make the bar chart gradient from small to large, you can specify the order of the cities and set the corresponding color mapping.

Average the salaries of each city and sort them from smallest to largest to get the list city_order

city_order = df.groupby("city") ["salary"].mean()\
               .sort_values()\
               .index.tolist()
Copy the code

Then set the order and color using the Order and Palette in Seaborn, respectively

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax,
           order = city_order,palette = "RdBu_r")
ax.set_title('Comparison of Salary levels by city')

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)
plt.box(False)
Copy the code

This is followed by adding grid lines on the yaxis to make it easier to see the size of each column. Since the grid lines are grid on the Y axis, set them with ax.yaxi.grid ()

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax,
           order = city_order,palette = "RdBu_r")
ax.set_title('Comparison of Salary levels by city')

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)
plt.box(False)

Set the Y-axis grid line
ax.yaxis.grid(linewidth=0.5,color='black')
# Put the grid line at the bottom
ax.set_axisbelow(True)
Copy the code

Since the meaning of x axis and y axis is relatively clear, you can remove the horizontal and vertical labels. At the same time, for more intuitive, you can change the scale labels of y axis from 20,15… With 20 k, 15 k…

This process uses ax.set_xlabel(),ax.set_ylabel(), and Ax.set_yticklabels (), respectively.

fig,ax = plt.subplots(figsize=(9.6))
sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax,
           order = city_order,palette = "RdBu_r")
ax.set_title('Comparison of Salary levels by city')

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)
plt.box(False)

Set the Y-axis grid line
ax.yaxis.grid(linewidth=0.5,color='black')
# Put the grid line on the bottom layer,
ax.set_axisbelow(True)

ax.set_xlabel(' ')
ax.set_ylabel(' ')
# set 0 to an empty string and add k everywhere else
ax.set_yticklabels([""."5k"."10k"."15k"."20k"])
Copy the code

Finally, set the title to make the title more beautiful. This step is mainly to adjust the parameters in Ax.set_title (), mainly including

  • backgroundcolor: Controls the background color
  • fontsize: Controls the font size
  • weight: Controls the font thickness
  • color: Controls the font color
fig,ax = plt.subplots(figsize=(9.6))

sns.barplot(x='city',y='salary',data=df,ci=95,ax=ax,
           order = city_order,palette = "RdBu_r")

The # font is 16px and the scale line is 0
ax.tick_params(labelsize=16,length=0)
plt.box(False)

Set the Y-axis grid line
ax.yaxis.grid(linewidth=0.5,color='black')
# Put the grid line on the bottom layer,
ax.set_axisbelow(True)

ax.set_xlabel(' ')
ax.set_ylabel(' ')
# set 0 to an empty string and add k everywhere else
ax.set_yticklabels([""."5k"."10k"."15k"."20k"])

ax.set_title('Comparison of Salary levels by city',backgroundcolor='#3c7f99',
            fontsize=24, weight='bold',color='white')
Copy the code

I’m going to give you a little bit of detail, so it might seem a little bit complicated to draw, but I’m only going to end up with 10 lines of code, but there’s a lot of detail. If the previous two related articles to understand, I believe that this is not difficult to understand.

Well, from now on, your Matplotlib drawings will be different!

Follow my official account “Python Reading Money” and reply “py” in the background to get the Python learning resources package