本篇详细介绍matplotlib.pyplot绘图方式中坐标轴(axis),刻度值(trick label),刻度(tricks),子图标题(title),图标题(suptitle),坐标轴标题(xlabel,ylabel),网格线(grid)等参数的详细设置,不过相对于官网还只是
冰山一角
。
1、详细代码
想看某个参数的作用,修改之后即可查看效果。
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(dpi=150)
plt.suptitle('整张图figure的标题:suptitle',
x=0.5,
y=0.98,
size=15,
ha='center',
va='top',
weight='bold',
rotation=1,
plt.subplot(1,1,1)
font_self = {'family':'Microsoft YaHei',
'fontsize': 10,
'fontweight' : 'bold',
'color' : (.228,.21,.28),
plt.title('每个子图axes的标题:title',
fontdict=font_self,
loc='left',
pad=7,
rotation=360,
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_visible(True)
plt.gca().spines['left'].set_visible(True)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['bottom'].set_color('black')
plt.gca().spines['bottom'].set_linewidth(10)
plt.gca().spines['bottom'].set_linestyle('--')
plt.grid()
plt.tick_params(axis='x',
which='both',
colors='r',
top='on',
bottom='on',
left='on',
right='on',
direction='out',
length=10,
width=2,
pad=10,
labelsize=10,
labelcolor='#008856',
zorder=0,
labeltop='on',
labelbottom='on',
labelleft='on',
labelright='off',
labelrotation=45,
grid_color='pink',
grid_alpha=1,
grid_linewidth=10,
grid_linestyle='-',
plt.xticks(np.arange(0, 2, step=0.2),list('abcdefghigk'),rotation=45)
plt.xlim(0,2)
plt.ylim(0,2)
from matplotlib.pyplot import MultipleLocator
plt.gca().xaxis.set_major_locator(MultipleLocator(0.2))
plt.gca().xaxis.set_minor_locator(MultipleLocator(0.1))
plt.xlabel('X轴标题',
labelpad=22,
fontdict=font_self,
rotation=90
以上代码结果
2、补充资料1:plt.gca()的所有属性(400+种)
属性(400+种)如下,感兴趣可以慢慢玩: ['__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__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_text', '_adjustable', '_agg_filter', '_alpha', '_anchor', '_animated', '_aspect', '_autoscaleXon', '_autoscaleYon', '_autotitlepos', '_axes', '_axes_class', '_axes_locator', '_axisbelow', '_clipon', '_clippath', '_connected', '_contains', '_convert_dx', '_current_image', '_facecolor', '_frameon', '_gci', '_gen_axes_patch', '_gen_axes_spines', '_get_axis_list', '_get_clipping_extent_bbox', '_get_lines', '_get_patches_for_fill', '_get_view', '_gid', '_gridOn', '_in_layout', '_init_axis', '_label', '_layoutbox', '_left_title', '_make_twin_axes', '_mouseover', '_mouseover_set', '_navigate', '_navigate_mode', '_oid', '_on_units_changed', '_originalPosition', '_parse_scatter_color_args', '_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', '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', 'bar', 'barbs', 'barh', 'bbox', 'boxplot', 'broken_barh', 'bxp', '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', 'dataLim', 'drag_pan', 'draw', 'draw_artist', 'end_pan', 'errorbar', 'eventplot', 'eventson', 'figbox', 'figure', 'fill', 'fill_between', 'fill_betweenx', 'findobj', 'fmt_xdata', 'fmt_ydata', 'format_coord', 'format_cursor_data', 'format_xdata', 'format_ydata', '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', 'has_data', 'have_units', 'hexbin', 'hist', 'hist2d', 'hlines', 'ignore_existing_data_limits', 'images', 'imshow', 'in_axes', 'indicate_inset', 'indicate_inset_zoom', 'inset_axes', 'invert_xaxis', 'invert_yaxis', 'is_first_col', 'is_first_row', 'is_last_col', 'is_last_row', 'is_transform_set', 'label_outer', 'legend', 'legend_', 'lines', 'locator_params', 'loglog', 'magnitude_spectrum', 'margins', 'matshow', 'minorticks_off', 'minorticks_on', 'mouseover', 'mouseover_set', 'name', 'numCols', 'numRows', 'patch', 'patches', 'pchanged', 'pcolor', 'pcolorfast', 'pcolormesh', 'phase_spectrum', 'pick', 'pickable', 'pie', 'plot', 'plot_date', 'properties', 'psd', 'quiver', 'quiverkey', 'redraw_in_frame', 'relim', 'remove', 'remove_callback', 'reset_position', 'rowNum', 'scatter', 'secondary_xaxis', 'secondary_yaxis', 'semilogx', 'semilogy', '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', 'specgram', 'spines', 'spy', 'stackplot', 'stale', 'stale_callback', 'start_pan', 'stem', 'step', 'sticky_edges', 'streamplot', 'table', 'tables', 'text', 'texts', 'tick_params', 'ticklabel_format', 'title', 'titleOffsetTrans', 'transAxes', 'transData', 'transLimits', 'transScale', 'tricontour', 'tricontourf', 'tripcolor', 'triplot', 'twinx', 'twiny', 'update', 'update_datalim', 'update_datalim_bounds', 'update_from', 'update_params', 'use_sticky_edges', 'viewLim', 'violin', 'violinplot', 'vlines', 'xaxis', 'xaxis_date', 'xaxis_inverted', 'xcorr', 'yaxis', 'yaxis_date', 'yaxis_inverted', 'zorder']
3、补充资料2:plt.gca().spines属性(200+)
属性(200+),感兴趣可以自己玩:['__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__', '_adjust_location', '_agg_filter', '_alias_map', '_alpha', '_animated', '_antialiased', '_axes', '_bind_draw_path_function', '_bounds', '_calc_offset_transform', '_capstyle', '_clipon', '_clippath', '_contains', '_convert_xy_units', '_dashes', '_dashoffset', '_edge_default', '_edgecolor', '_ensure_position_is_set', '_facecolor', '_fill', '_get_clipping_extent_bbox', '_gid', '_hatch', '_hatch_color', '_in_layout', '_joinstyle', '_label', '_linestyle', '_linewidth', '_mouseover', '_oid', '_original_edgecolor', '_original_facecolor', '_patch_transform', '_patch_type', '_path', '_path_effects', '_picker', '_position', '_process_radius', '_prop_order', '_propobservers', '_rasterized', '_recompute_transform', '_remove_method', '_set_edgecolor', '_set_facecolor', '_set_gc_clip', '_sketch', '_smart_bounds', '_snap', '_spine_transform', '_stale', '_sticky_edges', '_transform', '_transformSet', '_url', '_us_dashes', '_visible', 'add_callback', 'aname', 'arc_spine', 'axes', 'axis', 'circular_spine', 'cla', 'clipbox', 'contains', 'contains_point', 'contains_points', 'convert_xunits', 'convert_yunits', 'draw', 'eventson', 'figure', 'fill', 'findobj', 'format_cursor_data', 'get_aa', 'get_agg_filter', 'get_alpha', 'get_animated', 'get_antialiased', 'get_bounds', 'get_capstyle', 'get_children', 'get_clip_box', 'get_clip_on', 'get_clip_path', 'get_contains', 'get_cursor_data', 'get_data_transform', 'get_ec', 'get_edgecolor', 'get_extents', 'get_facecolor', 'get_fc', 'get_figure', 'get_fill', 'get_gid', 'get_hatch', 'get_in_layout', 'get_joinstyle', 'get_label', 'get_linestyle', 'get_linewidth', 'get_ls', 'get_lw', 'get_patch_transform', 'get_path', 'get_path_effects', 'get_picker', 'get_position', 'get_rasterized', 'get_sketch_params', 'get_smart_bounds', 'get_snap', 'get_spine_transform', 'get_tightbbox', 'get_transform', 'get_transformed_clip_path_and_affine', 'get_url', 'get_verts', 'get_visible', 'get_window_extent', 'get_zorder', 'have_units', 'is_frame_like', 'is_transform_set', 'linear_spine', 'mouseover', 'pchanged', 'pick', 'pickable', 'properties', 'register_axis', 'remove', 'remove_callback', 'set', 'set_aa', 'set_agg_filter', 'set_alpha', 'set_animated', 'set_antialiased', 'set_bounds', 'set_capstyle', 'set_clip_box', 'set_clip_on', 'set_clip_path', 'set_color', 'set_contains', 'set_ec', 'set_edgecolor', 'set_facecolor', 'set_fc', 'set_figure', 'set_fill', 'set_gid', 'set_hatch', 'set_in_layout', 'set_joinstyle', 'set_label', 'set_linestyle', 'set_linewidth', 'set_ls', 'set_lw', 'set_patch_arc', 'set_patch_circle', 'set_patch_line', 'set_path_effects', 'set_picker', 'set_position', 'set_rasterized', 'set_sketch_params', 'set_smart_bounds', 'set_snap', 'set_transform', 'set_url', 'set_visible', 'set_zorder', 'spine_type', 'stale', 'stale_callback', 'sticky_edges', 'update', 'update_from', 'validCap', 'validJoin', 'zorder']
4、参考资料
matplotlib.org/api/_as_gen…
matplotlib.org/api/_as_gen…
matplotlib.org/api/text_ap…
matplotlib.org/tutorials/i…
matplotlib.org/api/_as_gen…
matplotlib.org/api/spines_…
欢迎关注公众号:pythonic生物人
pythonic生物人
中级数据挖掘工程师
36.8k
粉丝