1.导包
import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
% matplotlib inline
plt. rcParams[ 'font.sans-serif' ] = "SimHei"
plt. rcParams[ "axes.unicode_minus" ] = False
% config Inlinebackend. figure_format = "svg"
2.绘图基本属性
plt. figure( figsize= ( 6 , 4 ) )
x = np. linspace( 0 , 2 * np. pi)
plt. plot( x, np. sin( x) , label= "sin" )
plt. plot( x, np. cos( x) , label= "cos" )
plt. legend( )
<matplotlib.legend.Legend at 0x2b222b11450>
plt. figure( figsize= ( 6 , 4 ) )
x = np. linspace( 0 , 2 * np. pi)
plt. plot( x, np. sin( x) )
plt. plot( x, np. cos( x) )
plt. legend( [ "sin" , "cos" ] , fontsize= 12 , loc= "center" , ncol= 2 , bbox_to_anchor= [ 0 , 1 , 1 , 0.2 ] )
<matplotlib.legend.Legend at 0x2b224435150>
3.线条属性
color 颜色 linestyle 样式 linewidth 宽度 alpha 透明度 marker 标记 mfc:marker face color 标记的背景颜色
plt. figure( figsize= ( 6 , 4 ) )
x = np. linspace( 0 , 2 * np. pi, 20 )
y1 = np. sin( x)
y2 = np. cos( x)
"""
**Markers**
============= ===============================
character description
============= ===============================
``'.'`` 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
``'8'`` octagon marker
``'s'`` square marker
``'p'`` pentagon marker
``'P'`` plus (filled) marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'X'`` x (filled) marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
**Line Styles**
============= ===============================
character description
============= ===============================
``'-'`` solid line style
``'--'`` dashed line style
``'-.'`` dash-dot line style
``':'`` dotted line style
============= ===============================
Example format strings::
'b' # blue markers with default shape
'or' # red circles
'-g' # green solid line
'--' # dashed line with default color
'^k:' # black triangle_up markers connected by a dotted line
**Colors**
The supported color abbreviations are the single letter codes
============= ===============================
character color
============= ===============================
``'b'`` blue
``'g'`` green
``'r'`` red
``'c'`` cyan
``'m'`` magenta
``'y'`` yellow
``'k'`` black
``'w'`` white
============= ===============================
"""
plt. plot( x, y1, c= "r" , marker= "o" , ls= "--" , lw= 1 , label= "sinx" , mfc= "y" )
plt. plot( x, y2, c= "b" , marker= "*" , ls= "-" , lw= 2 , label= "cosx" , mfc= "w" , markersize= 10 )
plt. plot( x, y1- y2, c= "y" , marker= "^" , ls= "-" , lw= 3 , label= "sinx-cosx" , mfc= "b" , markersize= 10 )
plt. plot( x, y1+ y2, c= "orange" , marker= ">" , ls= "-." , lw= 4 , label= "sinx+cosx" , mfc= "y" , markersize= 10 , markeredgecolor= "green" , markeredgewidth= 2 , alpha= 0.5 )
plt. legend( )
<matplotlib.legend.Legend at 0x218151a2f50>
4.坐标轴刻度
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 )
y = np. sin( x)
plt. plot( x, y)
plt. xticks( np. arange( 0 , 11 , 1 ) )
plt. yticks( [ - 1 , 0 , 1 ] )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 )
y = np. sin( x)
plt. plot( x, y)
plt. xticks( ticks= np. arange( 0 , 11 , 1 ) , fontsize= 20 , color= "red" )
plt. yticks( ticks= [ - 1 , 0 , 1 ] , labels= [ "min" , "0" , "max" ] , fontsize= 20 , color= "blue" , ha= "right" )
plt. show( )
5.坐标轴范围
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. xlim( - 2 , 8 )
plt. ylim( - 2 , 2 )
plt. show( )
6.坐标轴配置
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( [ - 2 , 8 , - 2 , 2 ] )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "on" )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "off" )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "equal" )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "scaled" )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "tight" )
plt. show( )
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. axis( "square" )
plt. show( )
7.标题和网格线
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 )
y = np. sin( x)
plt. plot( x, y, c= "r" )
plt. title( "sin曲线" , fontsize= 15 , loc= "center" )
plt. suptitle( "父标题" , y= 1.1 , fontsize= 20 )
plt. grid( ls= "--" , lw= 0.5 , c= "gray" , axis= "y" )
8.标签
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 )
y = np. sin( x)
plt. plot( x, y)
plt. xlabel( "y=sin(x)" , fontsize= 20 , rotation= 30 )
plt. ylabel( "y=sin(x)" , fontsize= 20 , rotation= 30 , horizontalalignment= "right" )
plt. title( "正弦曲线" )
plt. show( )
9.文本
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 , 10 )
y = np. array( [ 60 , 30 , 20 , 90 , 40 , 60 , 50 , 80 , 70 , 30 ] )
plt. plot( x, y, ls= "--" , marker= "o" )
for a, b in zip ( x, y) :
plt. text( x= a+ 0.3 , y= b+ 1 , s= b, fontsize= 10 , color= "r" , ha= "center" , va= "center" )
10.注释
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 10 , 10 )
y = np. array( [ 60 , 30 , 20 , 90 , 40 , 60 , 50 , 80 , 70 , 30 ] )
plt. plot( x, y, ls= "--" , marker= "o" )
plt. annotate( text= "最高销量" , xy= ( 3.2 , 90 ) , xytext= ( 1 , 80 ) , arrowprops= { "width" : 1 , "headwidth" : 8 , "facecolor" : "blue" } )
Text(1, 80, '最高销量')
11.保存图片
plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
plt. plot( x, np. sin( x) )
plt. plot( x, np. cos( x) )
plt. savefig( "sincos.png" )
fig = plt. figure( figsize= ( 5 , 3 ) )
x = np. linspace( 0 , 2 * np. pi)
plt. plot( x, np. sin( x) )
plt. plot( x, np. cos( x) )
fig. savefig( fname= "sincos2.png" , dpi= 100 , facecolor= 'pink' , pad_inches= 1 )