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

After introducing the differences between PLT and AX drawing in the previous article, this article has put together some of the most common component Settings in Matplotlib based on my own experience.

Personally, I think it is not difficult to draw a map with Matplotlib, but the difficulty is that for some details, it may take a long time to deal with a small detail, so you can click on the collection and then see, when you need to find out in time to see.

First, the official drawing of each component in a picture of the noun explanation, through this picture you can intuitively feel what is legend, what is tick…… , you can also search the corresponding document according to the figure if you do not set it.

Note: the plots below were all generated using FIG,ax = plt.subplots()

Set the display of Chinese font

By default, Matplotlib displays Chinese as garbled ◻, as in the following case

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])
plt.show()
Copy the code

So you can solve this problem with the following code before drawing

plt.rcParams['font.sans-serif'] = ['SimHei']
# SimHei: Microsoft Yahei
# FangSong
# These two are the ones I often use, the others can be searched on the Internet

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])
plt.show()
Copy the code

Set title

Function: ax. Set_title

Font and title positions can be set using Fontdict and LOc parameters, respectively

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])

Set the font size to 16px and display the title on the left
ax.set_title('title',fontdict={'size':16},loc = 'left')
plt.show()
Copy the code

Spine display problem

Function: ax. Spines [loc] set_visible (False)

By default, Matplotlib will show a spine in the graph.

I usually call it a border, which is the top, bottom, left, and right sides of a graph, as shown here

In normal use, sometimes only the spine of the left and bottom is needed, and sometimes all four borders are not needed. You can set it like this:

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])
# Just the left and bottom borders
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
Copy the code

Legend Settings (Legend)

Function: ax. Legend ()

A legend is an explanation of what a graph shows. For example, if three lines are drawn on a graph, what do these three lines represent? This is where you need to make some comments.

There are two ways to display legends:

# 1:
Add label to # plot and then ax.legend()
fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5],label='2010')
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [2.4.6],label='2020')
ax.legend()
plt.show()

# 2:
# Use ax.legend() to set the legend in order
fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [2.4.6])
ax.legend(['2010'.'2020'])
plt.show()
Copy the code

The frameon and fontSize parameters set whether to display the border of the legend and the size of the text in the legend.

White space control between graphics and borders

Function: ax. Margins ()

I don’t know if you noticed when you were drawing, but by default Matplotlib has a blank space between what we’re drawing and the border, for example

To remove this white space, use ax.Margins ()

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5])
ax.fill_between(['Beijing'.'Shanghai'.'shenzhen'], [1.1.1], [1.3.5],color='#7b68ee')
# You can set the direction and width of the white space separately
ax.margins(0)
plt.show()
Copy the code

Set two axes

Function: ax. Twinx ()

Double coordinate axes are generally used for composite charts, and the indicators of the two charts have different dimensions. The classic use scenario is pareto chart.

In use, ax2 needs to be generated using.twinx() method for the original AX, and then ax2 is used for drawing

fig,ax = plt.subplots()
ax.plot(['Beijing'.'Shanghai'.'shenzhen'], [1.3.5],color='r')

# Double coordinate usage
ax2 = ax.twinx()
ax2.bar(['Beijing'.'Shanghai'.'shenzhen'], [20.40.60],alpha=0.3)

plt.show()
Copy the code

Coordinate axis Settings

In a two-dimensional diagram, terms relating to the components of an axis are shown

First, there are xaxis and Yaxis (note to distinguish from AXES). The labels (or names of axes) on the axes are XLabel and Ylabel. There is a scale line tick on the axes, and the corresponding scale label on the scale is tick label.

The corresponding function is

  • xlabel –> ax.set_xlabel()
  • ylabel –> ax.set_ylabel()
  • Tick and TICK Label –>ax.tick_params.ax.xticks().ax.yticks()
fig,ax = plt.subplots()
ax.scatter([3.2.1], [1.3.5],color='r')
ax.plot([3.2.1], [1.3.5],color='r')

# set the xy axis label separately
ax.set_xlabel('x',fontsize=16)
ax.set_ylabel('y',fontsize=16)
Copy the code

fig,ax = plt.subplots()
ax.scatter([3.2.1], [1.3.5],color='r')
ax.plot([3.2.1], [1.3.5],color='r')

# fontsize Sets the scale label size
# direction controls whether the scale is displayed inward or outward
ax.tick_params(labelsize=14,direction='in')
Copy the code

Grid Settings

Function: ax. The grid ()

Grid lines are mainly used to assist in viewing the specific value size. The horizontal and vertical coordinates can be set corresponding grid lines, depending on the specific situation.

fig,ax = plt.subplots()
ax.plot([1.2.3], [4.2.6],color='r')
The # b parameter sets whether to display the grid
The axis parameter sets which axis to display grid lines on. The optional parameters are 'x','y','both'.
ax.grid(b=True,axis='y')
Copy the code

Commonly used chart component Settings are summarized here, there is a wrong place to clap brick!

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