import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib. pyplot as plt
% matplotlib inline
s1 = Series( np. random. randint( 100 , 150 , size= 20 ) )
s1
0 127
1 124
2 143
3 139
4 139
5 145
6 147
7 132
8 106
9 117
10 123
11 136
12 113
13 118
14 141
15 131
16 109
17 149
18 136
19 101
dtype: int32
s1. plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x22ed370>
df1 = DataFrame( np. random. randint( 0 , 100 , size= ( 20 , 3 ) ) , columns= [ 'SQLite' , 'Oracle' , 'MySQL' ] )
df1
SQLite Oracle MySQL 0 45 54 9 1 52 63 0 2 44 97 35 3 49 51 95 4 35 51 27 5 50 80 73 6 89 17 22 7 87 32 15 8 32 93 60 9 54 12 54 10 7 93 21 11 81 99 81 12 32 80 47 13 42 34 27 14 57 3 98 15 49 0 78 16 15 66 13 17 65 66 5 18 45 7 99 19 53 98 24
df1. plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x1878550>
df1. sort_values( 'SQLite' )
SQLite Oracle MySQL 10 7 93 21 16 15 66 13 8 32 93 60 12 32 80 47 4 35 51 27 13 42 34 27 2 44 97 35 0 45 54 9 18 45 7 99 3 49 51 95 15 49 0 78 5 50 80 73 1 52 63 0 19 53 98 24 9 54 12 54 14 57 3 98 17 65 66 5 11 81 99 81 7 87 32 15 6 89 17 22
df1. sort_values( 'SQLite' , ascending= False )
SQLite Oracle MySQL 6 89 17 22 7 87 32 15 11 81 99 81 17 65 66 5 14 57 3 98 9 54 12 54 19 53 98 24 1 52 63 0 5 50 80 73 3 49 51 95 15 49 0 78 18 45 7 99 0 45 54 9 2 44 97 35 13 42 34 27 4 35 51 27 12 32 80 47 8 32 93 60 16 15 66 13 10 7 93 21
df1. sort_values( 'SQLite' , ascending= False ) . plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x1907cd0>
df1. sort_values( 'SQLite' , ascending= False ) . reset_index( drop= True ) . plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x6862c30>
df1. sort_values( [ 'SQLite' , 'Oracle' ] , ascending= False ) . reset_index( drop= True ) . plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x7ba0eb0>
df1. sort_values( [ 'SQLite' , 'Oracle' ] , ascending= False ) . reset_index( drop= True ) [ [ 'SQLite' , 'Oracle' ] ] . plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x9598870>
df1. plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x962ae10>
柱状图
kind=‘bar’ 垂直方向 kind=‘barh’ 水平方向
s1. plot( )
<matplotlib.axes._subplots.AxesSubplot at 0x969f7f0>
s1. plot( kind= 'bar' )
<matplotlib.axes._subplots.AxesSubplot at 0x975faf0>
s1. plot( kind= 'bar' , colormap= 'rainbow' )
<matplotlib.axes._subplots.AxesSubplot at 0x9842dd0>
from matplotlib. colors import ListedColormap
cm1 = ListedColormap( [ np. random. uniform( 0 , 1 , size= 3 ) for i in range ( 3 ) ] )
cm2 = ListedColormap( [ 'red' , 'blue' , 'green' ] )
s1. plot( kind= 'bar' , colormap= cm1)
<matplotlib.axes._subplots.AxesSubplot at 0xa1a1bb0>
s1. plot( kind= 'bar' , colormap= cm2)
<matplotlib.axes._subplots.AxesSubplot at 0xa27f190>
df1
SQLite Oracle MySQL 0 45 54 9 1 52 63 0 2 44 97 35 3 49 51 95 4 35 51 27 5 50 80 73 6 89 17 22 7 87 32 15 8 32 93 60 9 54 12 54 10 7 93 21 11 81 99 81 12 32 80 47 13 42 34 27 14 57 3 98 15 49 0 78 16 15 66 13 17 65 66 5 18 45 7 99 19 53 98 24
df1. plot( kind= 'bar' )
<matplotlib.axes._subplots.AxesSubplot at 0xa3a1bb0>
df1. plot( kind= 'bar' , colormap= cm1)
<matplotlib.axes._subplots.AxesSubplot at 0xb4ece90>
df1. plot( kind= 'bar' , colormap= cm2)
<matplotlib.axes._subplots.AxesSubplot at 0xb5a4650>
plt. rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
plt. rcParams[ 'axes.unicode_minus' ] = False
df1. plot( kind= 'bar' , colormap= cm2, title= '20位同学的期中考试成绩' )
<matplotlib.axes._subplots.AxesSubplot at 0xb7c54b0>
df1. plot( kind= 'bar' , colormap= cm2, title= '20位同学的期中考试成绩' , fontsize= 20 )
<matplotlib.axes._subplots.AxesSubplot at 0xb9360b0>
ax = df1. plot( kind= 'bar' , colormap= cm2, title= '20位同学的期中考试成绩' , fontsize= 20 )
hasattr ( ax, 'set_title' )
True
hasattr ( ax, 'title' )
True
ax. title
Text(0.5, 1, '20位同学的期中考试成绩')
ax. title. get_position( )
(0.5, 1)
dir ( ax. title)
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_agg_filter',
'_alias_map',
'_alpha',
'_animated',
'_axes',
'_bbox_patch',
'_cached',
'_clipon',
'_clippath',
'_color',
'_contains',
'_draw_bbox',
'_fontproperties',
'_get_clipping_extent_bbox',
'_get_dist_to_box',
'_get_layout',
'_get_multialignment',
'_get_rendered_text_width',
'_get_wrap_line_width',
'_get_wrapped_text',
'_get_xy_display',
'_gid',
'_horizontalalignment',
'_in_layout',
'_label',
'_linespacing',
'_mouseover',
'_multialignment',
'_oid',
'_path_effects',
'_picker',
'_preprocess_math',
'_prop_order',
'_propobservers',
'_rasterized',
'_remove_method',
'_renderer',
'_rotation',
'_rotation_mode',
'_set_gc_clip',
'_sketch',
'_snap',
'_stale',
'_sticky_edges',
'_text',
'_transform',
'_transformSet',
'_update_clip_properties',
'_url',
'_usetex',
'_verticalalignment',
'_visible',
'_wrap',
'_x',
'_y',
'add_callback',
'aname',
'axes',
'clipbox',
'contains',
'convert_xunits',
'convert_yunits',
'draw',
'eventson',
'figure',
'findobj',
'format_cursor_data',
'get_agg_filter',
'get_alpha',
'get_animated',
'get_bbox_patch',
'get_c',
'get_children',
'get_clip_box',
'get_clip_on',
'get_clip_path',
'get_color',
'get_contains',
'get_cursor_data',
'get_family',
'get_figure',
'get_font_properties',
'get_fontfamily',
'get_fontname',
'get_fontproperties',
'get_fontsize',
'get_fontstyle',
'get_fontvariant',
'get_fontweight',
'get_gid',
'get_ha',
'get_horizontalalignment',
'get_in_layout',
'get_label',
'get_name',
'get_path_effects',
'get_picker',
'get_position',
'get_prop_tup',
'get_rasterized',
'get_rotation',
'get_rotation_mode',
'get_size',
'get_sketch_params',
'get_snap',
'get_stretch',
'get_style',
'get_text',
'get_tightbbox',
'get_transform',
'get_transformed_clip_path_and_affine',
'get_unitless_position',
'get_url',
'get_usetex',
'get_va',
'get_variant',
'get_verticalalignment',
'get_visible',
'get_weight',
'get_window_extent',
'get_wrap',
'get_zorder',
'have_units',
'is_math_text',
'is_transform_set',
'mouseover',
'pchanged',
'pick',
'pickable',
'properties',
'remove',
'remove_callback',
'set',
'set_agg_filter',
'set_alpha',
'set_animated',
'set_backgroundcolor',
'set_bbox',
'set_c',
'set_clip_box',
'set_clip_on',
'set_clip_path',
'set_color',
'set_contains',
'set_family',
'set_figure',
'set_font_properties',
'set_fontfamily',
'set_fontname',
'set_fontproperties',
'set_fontsize',
'set_fontstretch',
'set_fontstyle',
'set_fontvariant',
'set_fontweight',
'set_gid',
'set_ha',
'set_horizontalalignment',
'set_in_layout',
'set_label',
'set_linespacing',
'set_ma',
'set_multialignment',
'set_name',
'set_path_effects',
'set_picker',
'set_position',
'set_rasterized',
'set_rotation',
'set_rotation_mode',
'set_size',
'set_sketch_params',
'set_snap',
'set_stretch',
'set_style',
'set_text',
'set_transform',
'set_url',
'set_usetex',
'set_va',
'set_variant',
'set_verticalalignment',
'set_visible',
'set_weight',
'set_wrap',
'set_x',
'set_y',
'set_zorder',
'stale',
'stale_callback',
'sticky_edges',
'update',
'update_bbox_position_size',
'update_from',
'zorder']
[ attr for attr in dir ( ax. title) if not attr. startswith( '_' ) ]
['add_callback',
'aname',
'axes',
'clipbox',
'contains',
'convert_xunits',
'convert_yunits',
'draw',
'eventson',
'figure',
'findobj',
'format_cursor_data',
'get_agg_filter',
'get_alpha',
'get_animated',
'get_bbox_patch',
'get_c',
'get_children',
'get_clip_box',
'get_clip_on',
'get_clip_path',
'get_color',
'get_contains',
'get_cursor_data',
'get_family',
'get_figure',
'get_font_properties',
'get_fontfamily',
'get_fontname',
'get_fontproperties',
'get_fontsize',
'get_fontstyle',
'get_fontvariant',
'get_fontweight',
'get_gid',
'get_ha',
'get_horizontalalignment',
'get_in_layout',
'get_label',
'get_name',
'get_path_effects',
'get_picker',
'get_position',
'get_prop_tup',
'get_rasterized',
'get_rotation',
'get_rotation_mode',
'get_size',
'get_sketch_params',
'get_snap',
'get_stretch',
'get_style',
'get_text',
'get_tightbbox',
'get_transform',
'get_transformed_clip_path_and_affine',
'get_unitless_position',
'get_url',
'get_usetex',
'get_va',
'get_variant',
'get_verticalalignment',
'get_visible',
'get_weight',
'get_window_extent',
'get_wrap',
'get_zorder',
'have_units',
'is_math_text',
'is_transform_set',
'mouseover',
'pchanged',
'pick',
'pickable',
'properties',
'remove',
'remove_callback',
'set',
'set_agg_filter',
'set_alpha',
'set_animated',
'set_backgroundcolor',
'set_bbox',
'set_c',
'set_clip_box',
'set_clip_on',
'set_clip_path',
'set_color',
'set_contains',
'set_family',
'set_figure',
'set_font_properties',
'set_fontfamily',
'set_fontname',
'set_fontproperties',
'set_fontsize',
'set_fontstretch',
'set_fontstyle',
'set_fontvariant',
'set_fontweight',
'set_gid',
'set_ha',
'set_horizontalalignment',
'set_in_layout',
'set_label',
'set_linespacing',
'set_ma',
'set_multialignment',
'set_name',
'set_path_effects',
'set_picker',
'set_position',
'set_rasterized',
'set_rotation',
'set_rotation_mode',
'set_size',
'set_sketch_params',
'set_snap',
'set_stretch',
'set_style',
'set_text',
'set_transform',
'set_url',
'set_usetex',
'set_va',
'set_variant',
'set_verticalalignment',
'set_visible',
'set_weight',
'set_wrap',
'set_x',
'set_y',
'set_zorder',
'stale',
'stale_callback',
'sticky_edges',
'update',
'update_bbox_position_size',
'update_from',
'zorder']
ax = df1. plot( kind= 'bar' , colormap= cm2, title= '20位同学的期中考试成绩' , fontsize= 20 )
ax. title. set_size( 20 )
plt. show( )
ax = df1. plot( kind= 'bar' , colormap= cm2, title= '20位同学的期中考试成绩' , fontsize= 20 )
ax. title. set_size( 20 )
ax. title. set_position( ( 0.5 , 1 , 1 ) )
plt. show( )
fig, axes = plt. subplots( 2 , 1 , figsize= ( 8 , 10 ) )
s1. plot( kind= 'bar' , ax= axes[ 0 ] , title= '图1' )
s1. plot( kind= 'barh' , ax= axes[ 1 ] , title= '图2' )
plt. show( )
df1. plot( kind= 'bar' )
<matplotlib.axes._subplots.AxesSubplot at 0xbfe0930>
fig, axes = plt. subplots( figsize= ( 10 , 6 ) )
df1. plot( kind= 'bar' , ax= axes)
<matplotlib.axes._subplots.AxesSubplot at 0xc22da50>
fig, axes = plt. subplots( figsize= ( 10 , 6 ) )
df1. plot( kind= 'bar' , ax= axes, stacked= True )
<matplotlib.axes._subplots.AxesSubplot at 0xbb404b0>
直方图或密度图
.hist(bins=10,alpha=0.5,color=“gray”,normed|density=True) .plot(kind=kde)
s1. hist( )
<matplotlib.axes._subplots.AxesSubplot at 0xbbedc30>
s1. hist( bins= 20 )
<matplotlib.axes._subplots.AxesSubplot at 0xbbb66b0>
s1. hist( bins= 2 )
<matplotlib.axes._subplots.AxesSubplot at 0xc495690>
s1. sort_values( )
19 101
8 106
16 109
12 113
9 117
13 118
10 123
1 124
0 127
15 131
7 132
18 136
11 136
3 139
4 139
14 141
2 143
5 145
6 147
17 149
dtype: int32
s2 = Series( np. random. randn( 100 ) )
s2. hist( )
<matplotlib.axes._subplots.AxesSubplot at 0xc535c30>
s2 = Series( np. random. randn( 100 ) )
s2. hist( bins= 50 )
<matplotlib.axes._subplots.AxesSubplot at 0xc58ca90>
s2 = Series( np. random. randn( 100 ) )
s2. hist( bins= 50 )
s2. plot( kind= 'kde' , color= 'red' )
<matplotlib.axes._subplots.AxesSubplot at 0xd720070>
s2 = Series( np. random. randn( 100 ) )
s2. hist( bins= 50 , alpha= 0.3 , density= True )
s2. plot( kind= 'kde' , color= 'red' )
<matplotlib.axes._subplots.AxesSubplot at 0xc3ad670>
散点图
plt.scatter(x,y,marksize=50,color=) dataframe.plot(x列,y列,kind=‘scatter’) pd.plotting.scatter_maxtrix(frame,ax,diagonal=‘hist|kde’,s=50,c=‘’) 散点矩阵图
df1
SQLite Oracle MySQL 0 45 54 9 1 52 63 0 2 44 97 35 3 49 51 95 4 35 51 27 5 50 80 73 6 89 17 22 7 87 32 15 8 32 93 60 9 54 12 54 10 7 93 21 11 81 99 81 12 32 80 47 13 42 34 27 14 57 3 98 15 49 0 78 16 15 66 13 17 65 66 5 18 45 7 99 19 53 98 24
df1. plot( 'SQLite' , 'Oracle' )
<matplotlib.axes._subplots.AxesSubplot at 0xd7b91b0>
df1. plot( 'SQLite' , 'Oracle' , kind= 'scatter' )
<matplotlib.axes._subplots.AxesSubplot at 0xd7fae90>
df1. plot( 'Oracle' , 'MySQL' , kind= 'scatter' )
<matplotlib.axes._subplots.AxesSubplot at 0xd832c50>
plt. scatter( df1. MySQL, df1. Oracle)
<matplotlib.collections.PathCollection at 0xd8af3f0>
plt. scatter( df1. MySQL, df1. Oracle, s= 100 )
<matplotlib.collections.PathCollection at 0xd7fa090>
plt. scatter( df1. MySQL, df1. Oracle, s= 100 , c= 'r' )
<matplotlib.collections.PathCollection at 0xd995bb0>
plt. scatter( df1. MySQL, df1. Oracle, s= 100 , c= 'k' )
<matplotlib.collections.PathCollection at 0xda0f6d0>
pd. plotting. scatter_matrix( df1, diagonal= 'hist' )
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0DA2A610>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DC02AB0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DC26B50>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0DC45C30>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DC66D10>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DC86DF0>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0DCA6ED0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DCC8FB0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0DCD3570>]],
dtype=object)
pd. plotting. scatter_matrix( df1, diagonal= 'kde' )
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0DA1A930>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E58BA10>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E5A8AF0>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0E5C8BD0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E5E8CB0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E609D90>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0E62AEF0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E64C9B0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0E64CF50>]],
dtype=object)
pd. plotting. scatter_matrix( df1, diagonal= 'kde' , c= 'r' )
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0E578E10>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F6F2BB0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F70EC90>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0F72FD70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F74EE50>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F76FF30>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0F78FA70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F7B1B50>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F7BC0F0>]],
dtype=object)
pd. plotting. scatter_matrix( df1, diagonal= 'kde' , c= 'r' , s= 80 )
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0E6E0CD0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F87AD90>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F8A4E50>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0F8C2F30>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F8E3A70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F902B50>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x0F925C30>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F944D10>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F94F2D0>]],
dtype=object)