Python-- drawing

1, save the image.

fig.savefig

First, create a canvas

1. Create a canvas and axes

In the Matplotlib, plt.figure class can be seen as a container capable of accommodating a variety of axes, graphics, text and labels. plt.Axes class is a scale and with a rectangular label will eventually contain all the visual graphic elements.

Here, fig representing a legend, ax represents a coordinate axis or axes Examples instance a set of coordinates.

%matplotlib inline

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()

x = np.linspace(0,10,1000)
ax.plot (x, np.sin (x))

 

2, the object-oriented style interfaces

# Object-oriented style 
# create graphical network 
# AX is an array comprising two Axes objects, i.e., two axes 
Fig, AX = plt.subplots (2 )

# Invoked on each subject Axes plot () method, are plotted sin () and COS () 
AX [0] .plot (X, np.sin (X))
Ax [ 1] .plot (x, np.cos (x))

 

3, Matlab style interfaces

plt.figure () # create graphics

# Matlib Interface Style 
# Create a first two sub-graphs, the axis is provided, is equal to Fig, AX = plt.subplot () 
plt.subplot (2,. 1,. 1 )  
plt.plot (x, np.sin (x))

# Create a first two sub-graphs, axes provided 
plt.subplot (2,. 1, 2 )
plt.plot (x, np.cos (x))

 

 

 

 

Second, the adjustment axes and lines

1, adjust the line color and style

 

Line style:

'-'       solid line style
'--'      dashed line style
'-.'      dash-dot line style
':'       dotted line style

 

colour:

'.'       point marker
','       pixel marker
'o'       circle marker
'v'       triangle_down marker
'^'       triangle_up marker
'<'       triangle_left marker
'>'       triangle_right marker
'1'       tri_down marker
'2'       tri_up marker
'3'       tri_left marker
'4'       tri_right marker
's'       square marker
'p'       pentagon marker
'*'       star marker
'h'       hexagon1 marker
'H'       hexagon2 marker
'+'       plus marker
'x'       x marker
'D'       diamond marker
'd'       thin_diamond marker
'|'       vline marker
'_'       hline marker

 

plt.axhline (y = 1, ls = '.', c-'yellow ') # to increase the horizontal

plt.axvline (x = 1, ls = '-', c = 'red') # increase the vertical line

 

2, adjusts the axes

(1) adjusting the lower limit of the coordinate axes:

plt.xlim () # equivalent ax.set_xlim ()

plt.ylim () # equivalent ax.set_ylim ()

plt.axis([xmin, xmax, ymin, ymax])

(2) setting tab pattern

plt.title () # set the graphic title, equivalent to ax.set_title ()

plt.xlabel (), plt.ylabel () # Set X, Y axis title, equivalent to ax.set_xlabel (), ax.set_ylabel ()

(3) Configuration Legend

plt.legend () # Create Legend

ax.legend(frameon=False, loc='epper left')

# Selection element displayed legend 
# manner a 
plt.legend (Lines [: 2], [ ' First ' , ' SECOND ' ])

# Second way 
plt.plot (X, Y [:, 0], label = ' Frist ' )
plt.plot(x, y[:,1], label='second')
plt.plot(x,y[:,2:])
plt.legend (gramealpha = 1, frameon = True) # default will ignore those elements without labels

 

Third, and more sub-graph

1, FIG. FIG.

plt.axes ([bottom, left, width, height] # [bottom coordinates, sit coordinates, width, height]

#xample1
ax1 = plt.axes()
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])

#example2
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4],
                                xticklabels=[], ylim=(-1.2, 1.2))
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4],
                                death = (1.2, 1.2 ))
x = np.linspace(0, 10)
ax1.plot (np.sin (x))
ax2.plot(np.cos(x))

2, a simple grid subgraph

plt.subplot (the number of rows, columns, an index value)

I in Range for (l, 7): 
  plt.subplot (2,3, I)
  plt.text (0.5, 0.5, STR ((2,3, I)),
      fontsizt = 18 is, HA = 'Center')

Fig = plt.figure () # plt.subplot_adjust can adjust the spacing between sub FIG fig.subplots_adjust (hspace = 0.4, wspace = 0.4 ) for I in Range (l, 7 ): ax=fig.add_subplot(2, 3, i)
   ax.text(0.5, 0.5, str((2,3,i)),
      fontstze=18, ha='center')

 Equivalent to

plt.subplots(2, 3, sharex='col', sharey='row')

#比较subplot & subplots

#subplots_addjust

 

Fourth, text and notes

ax.text (): text notes

ax.transData: x-axis and y-axis coordinate as the data tag
ax.transAxes: lower left corner as the origin of the coordinate axes, the coordinate axis in proportion to the size of the rendering coordinates.
fig.transFigure: graphic to the lower left corner as the origin, according to the proportion of the size of the graphics rendering coordinates.

fig, ax = plt.subplots(facecolor='lightgray')
ax.axis([0, 10, 0, 10])

ax.text(1, 5, ".data:(1,5)", transform=ax.transData)
ax.text(0.5, 0.2, ".Axes:(0.5, 0.2)", transform=ax.transAxes)
ax.text(0.5, 0.2, ".Figure:(0.5, 0.2)", transform=fig.transFigure)

plt.annotate (): Creating arrow

 

Reference: "Python Data Science Handbook"

 

Guess you like

Origin www.cnblogs.com/tonorth123/p/11669215.html