05 ,子圖 :多個 ax 進行畫圖,fig.add_subplot

1 ,畫子圖 : fig.add_subplot(1,3,1)

  1. 代碼 :
if __name__ == '__main__':
    # 查看所有參數 :
    # print(plt.rcParams.keys())
    # 通用設置 plt : 中文,正負號,寬度,顏色,樣式
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.rcParams['lines.linewidth'] = 2
    plt.rcParams['lines.color'] = "red"
    plt.rcParams['lines.linestyle'] = "-"

    # 子圖 :
    fig = plt.figure()
    ax01 = fig.add_subplot(1,3,1)
    ax02 = fig.add_subplot(1,3,2)
    ax03 = fig.add_subplot(1,3,3)

    # 全顯示 :
    plt.tight_layout()
    plt.show()
  1. 結果 :

2 ,圖片大小 : fig = plt.figure(figsize=(9,3))

  1. 代碼 :
if __name__ == '__main__':
    # 查看所有參數 :
    # print(plt.rcParams.keys())
    # 通用設置 plt : 中文,正負號,寬度,顏色,樣式
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.rcParams['lines.linewidth'] = 2
    plt.rcParams['lines.color'] = "red"
    plt.rcParams['lines.linestyle'] = "-"

    # 子圖 :
    fig = plt.figure(figsize=(9,3))
    ax01 = fig.add_subplot(1,3,1)
    ax02 = fig.add_subplot(1,3,2)
    ax03 = fig.add_subplot(1,3,3)

    # 全顯示 :
    plt.tight_layout()
    plt.show()
  1. 結果 :
    在這裏插入圖片描述

3 ,查看類所有方法,子圖所有方法 : dir(ax01)

  1. 爲什麼要查看 : 因爲子圖不會畫了
  2. 查看類信息 :
help(ax01)
  1. 查看勒種所有方法 :
dir(ax01)

1 ,魔術方法 :'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
2 ,半魔術 : '_add_text', '_adjustable', '_agg_filter', '_alpha', '_anchor', '_animated', '_aspect', '_autoscaleXon', '_autoscaleYon', '_autotitlepos', '_axes', '_axes_class', '_axes_locator', '_axisbelow', '_cachedRenderer', '_clipon', '_clippath', '_connected', '_contains', '_current_image', '_facecolor', '_frameon', '_gci', '_gen_axes_patch', '_gen_axes_spines', '_get_axis_list', '_get_lines', '_get_patches_for_fill', '_get_view', '_gid', '_gridOn', '_hold', '_in_layout', '_init_axis', '_label', '_layoutbox', '_left_title', '_make_twin_axes', '_mouseover', '_mouseover_set', '_navigate', '_navigate_mode', '_oid', '_on_units_changed', '_originalPosition', '_path_effects', '_pcolorargs', '_picker', '_position', '_poslayoutbox', '_process_unit_info', '_prop_order', '_propobservers', '_quiver_units', '_rasterization_zorder', '_rasterized', '_remove_legend', '_remove_method', '_right_title', '_sci', '_set_artist_props', '_set_gc_clip', '_set_lim_and_transforms', '_set_position', '_set_title_offset_trans', '_set_view', '_set_view_from_bbox', '_shared_x_axes', '_shared_y_axes', '_sharex', '_sharey', '_sketch', '_snap', '_stale', '_sticky_edges', '_subplotspec', '_tight', '_transform', '_transformSet', '_twinned_axes', '_update_image_limits', '_update_line_limits', '_update_patch_limits', '_update_title_position', '_update_transScale', '_url', '_use_sticky_edges', '_validate_converted_limits', '_visible', '_xaxis_transform', '_xcid', '_xmargin', '_yaxis_transform', '_ycid', '_ymargin', 
3 ,a 方法 : 'acorr', 'add_artist', 'add_callback', 'add_child_axes', 'add_collection', 'add_container', 'add_image', 'add_line', 'add_patch', 'add_table', 'aname', 'angle_spectrum', 'annotate', 'apply_aspect', 'arrow', 'artists', 'autoscale', 'autoscale_view', 'axes', 'axhline', 'axhspan', 'axis', 'axison', 'axvline', 'axvspan', 
4 ,b 方法 : 'bar', 'barbs', 'barh', 'bbox', 'boxplot', 'broken_barh', 'bxp', 
5 ,c 方法 : 'callbacks', 'can_pan', 'can_zoom', 'change_geometry', 'child_axes', 'cla', 'clabel', 'clear', 'clipbox', 'cohere', 'colNum', 'collections', 'containers', 'contains', 'contains_point', 'contour', 'contourf', 'convert_xunits', 'convert_yunits', 'csd', 
6 ,d 方法 : 'dataLim', 'drag_pan', 'draw', 'draw_artist', 
7 ,e 方法 : 'end_pan', 'errorbar', 'eventplot', 'eventson', 
8 ,f 方法 : 'figbox', 'figure', 'fill', 'fill_between', 'fill_betweenx', 'findobj', 'fmt_xdata', 'fmt_ydata', 'format_coord', 'format_cursor_data', 'format_xdata', 'format_ydata', 
9 ,g 方法 : 'get_adjustable', 'get_agg_filter', 'get_alpha', 'get_anchor', 'get_animated', 'get_aspect', 'get_autoscale_on', 'get_autoscalex_on', 'get_autoscaley_on', 'get_axes_locator', 'get_axisbelow', 'get_children', 'get_clip_box', 'get_clip_on', 'get_clip_path', 'get_contains', 'get_cursor_data', 'get_data_ratio', 'get_data_ratio_log', 'get_default_bbox_extra_artists', 'get_facecolor', 'get_fc', 'get_figure', 'get_frame_on', 'get_geometry', 'get_gid', 'get_gridspec', 'get_images', 'get_in_layout', 'get_label', 'get_legend', 'get_legend_handles_labels', 'get_lines', 'get_navigate', 'get_navigate_mode', 'get_path_effects', 'get_picker', 'get_position', 'get_rasterization_zorder', 'get_rasterized', 'get_renderer_cache', 'get_shared_x_axes', 'get_shared_y_axes', 'get_sketch_params', 'get_snap', 'get_subplotspec', 'get_tightbbox', 'get_title', 'get_transform', 'get_transformed_clip_path_and_affine', 'get_url', 'get_visible', 'get_window_extent', 'get_xaxis', 'get_xaxis_text1_transform', 'get_xaxis_text2_transform', 'get_xaxis_transform', 'get_xbound', 'get_xgridlines', 'get_xlabel', 'get_xlim', 'get_xmajorticklabels', 'get_xminorticklabels', 'get_xscale', 'get_xticklabels', 'get_xticklines', 'get_xticks', 'get_yaxis', 'get_yaxis_text1_transform', 'get_yaxis_text2_transform', 'get_yaxis_transform', 'get_ybound', 'get_ygridlines', 'get_ylabel', 'get_ylim', 'get_ymajorticklabels', 'get_yminorticklabels', 'get_yscale', 'get_yticklabels', 'get_yticklines', 'get_yticks', 'get_zorder', 'grid', 
10,h 方法 : 'has_data', 'have_units', 'hexbin', 'hist', 'hist2d', 'hitlist', 'hlines', 
11,i 方法 : 'ignore_existing_data_limits', 'images', 'imshow', 'in_axes', 'indicate_inset', 'indicate_inset_zoom', 'inset_axes', 'invert_xaxis', 'invert_yaxis', 'is_figure_set', 'is_first_col', 'is_first_row', 'is_last_col', 'is_last_row', 'is_transform_set', 
12,l 方法 : 'label_outer', 'legend', 'legend_', 'lines', 'locator_params', 'loglog', 
13,m 方法 : 'magnitude_spectrum', 'margins', 'matshow', 'minorticks_off', 'minorticks_on', 'mouseover', 'mouseover_set', 
14,n 方法 : 'name', 'numCols', 'numRows', 
15,p 方法 : 'patch', 'patches', 'pchanged', 'pcolor', 'pcolorfast', 'pcolormesh', 'phase_spectrum', 'pick', 'pickable', 'pie', 'plot', 'plot_date', 'properties', 'psd', 
16 ,q 方法 :'quiver', 'quiverkey', 
17 ,r 方法 :'redraw_in_frame', 'relim', 'remove', 'remove_callback', 'reset_position', 'rowNum', 
18 ,s 方法 :'scatter', 'semilogx', 'semilogy', 'specgram', 'spines', 'spy', 'stackplot', 'stale', 'stale_callback', 'start_pan', 'stem', 'step', 'sticky_edges', 'streamplot', 
19set 方法 :'set', 'set_adjustable', 'set_agg_filter', 'set_alpha', 'set_anchor',
		 'set_animated', 'set_aspect', 'set_autoscale_on', 'set_autoscalex_on', 
		 'set_autoscaley_on', 'set_axes_locator', 'set_axis_off', 'set_axis_on', 
		 'set_axisbelow', 'set_clip_box', 'set_clip_on', 'set_clip_path', 
		 'set_contains', 'set_facecolor', 'set_fc', 'set_figure', 'set_frame_on', 
		 'set_gid', 'set_in_layout', 'set_label', 'set_navigate', 'set_navigate_mode', 
		 'set_path_effects', 'set_picker', 'set_position', 'set_prop_cycle', 
		 'set_rasterization_zorder', 'set_rasterized', 'set_sketch_params', 'set_snap', 
		 'set_subplotspec', 'set_title', 'set_transform', 'set_url', 'set_visible', 
		 'set_xbound', 'set_xlabel', 'set_xlim', 'set_xmargin', 'set_xscale', 
		 'set_xticklabels', 'set_xticks', 'set_ybound', 'set_ylabel', 'set_ylim', 
		 'set_ymargin', 'set_yscale', 'set_yticklabels', 'set_yticks', 'set_zorder', 
20,t 方法 :'table', 'tables', 'text', 'texts', 'tick_params', 'ticklabel_format', 'title', 'titleOffsetTrans', 'transAxes', 'transData', 'transLimits', 'transScale', 'tricontour', 'tricontourf', 'tripcolor', 'triplot', 'twinx', 'twiny', 
21,u 方法 :'update', 'update_datalim', 'update_datalim_bounds', 'update_from', 'update_params', 'use_sticky_edges', 
22,v 方法 : 'viewLim', 'violin', 'violinplot', 'vlines', 
23,x 方法 : 'xaxis', 'xaxis_date', 'xaxis_inverted', 'xcorr', 
24,y 方法 : 'yaxis', 'yaxis_date', 'yaxis_inverted', 
25,z 方法 : 'zorder'

4 ,子圖中畫折線 :

  1. 代碼 :
if __name__ == '__main__':
    # 查看所有參數 :
    # print(plt.rcParams.keys())
    # 通用設置 plt : 中文,正負號,寬度,顏色,樣式
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.rcParams['lines.linewidth'] = 2
    plt.rcParams['lines.color'] = "red"
    plt.rcParams['lines.linestyle'] = "-"

    # 子圖 :
    fig = plt.figure(figsize=(12,4))
    ax01 = fig.add_subplot(1,3,1)
    ax02 = fig.add_subplot(1,3,2)
    ax03 = fig.add_subplot(1,3,3)

    # 數據 :
    data = pd.read_csv("UNRATE.csv")
    data01 = data.loc[0:12]
    data02 = data.loc[12:24]
    data03 = data.loc[24:36]

    # 子圖中畫折線 :
    ax01.plot(data01["DATE"],data01["VALUE"])
    ax02.plot(data02["DATE"],data02["VALUE"])
    ax03.plot(data03["DATE"],data03["VALUE"])

    # 子圖小標題
    ax01.set_title("1948")
    ax02.set_title("1949")
    ax03.set_title("1950")

    # 子圖文字傾斜
    ax01.set_xticklabels(labels=data01["DATE"],rotation=45)
    ax02.set_xticklabels(labels=data02["DATE"],rotation=45)
    ax03.set_xticklabels(labels=data03["DATE"],rotation=45)

    # 子圖座標名字
    ax01.set_xlabel("年份")
    ax01.set_ylabel("失業率")
    ax02.set_xlabel("年份")
    ax02.set_ylabel("失業率")
    ax03.set_xlabel("年份")
    ax03.set_ylabel("失業率")

    # 全顯示 :
    plt.tight_layout()
    plt.show()
  1. 結果 :
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章