这篇教程python 如何设置柱状图参数写得很实用,希望能帮到您。 version:python 3.6 环境:anaconda/JupyterLab 0.27.0 操作系统:Windows 10 import pandas as pdimport matplotlib.pyplot as plta = pd.DataFrame(train_set['收率'].value_counts()).reset_index()a.rename(columns={'index': 'yield','收率':'frequency'}, inplace=True)a.head()plt.figure(figsize=(24,8))plt.bar(a['yield'].values,a['frequency'].values.round(2),color='rgb',width = 0.005,tick_label=a['yield'].values.round(3))plt.xlabel('yield', fontsize=20)plt.ylabel('frequency', fontsize=20)plt.show() 
补充:python 用 matplotlib 绘制柱状图参数详解 plt.bar() 1、加载库import numpy as npimport matplotlib as mplimport matplotlib.pyplot as plt 2 绘图,逐步设置 bar() 参数def title_table(ax): '''为图表添加标题和表格''' ax.set_title(label=f'No.{i+1}', loc='center', pad=None, fontdict={'color': 'b'} ) ax.table(loc='upper right', # 表格在图表区的位置 colLabels=[f'{i+2} args'], # 表格每列的列名称 colColours=['g'], # 表格每列列名称所在单元格的填充颜色 colLoc='left', # 表格中每列列名称的对齐位置 colWidths=[0.15], # 表格每列的宽度 cellText=args, # 表格中的数值, 每行数据的列表的列表 cellColours=[['cornsilk']]*len(args), # 表格中数据所在单元格的填充颜色 cellLoc='left', # 表格中数据的对齐位置 fontsize=8)# 配置字体,显示中文mpl.rcParams['font.sans-serif'] = ['SimHei'] # 配置坐标轴刻度值模式,显示负号mpl.rcParams['axes.unicode_minus'] = True# 定义数据x = [1, 2, 3, 4, 5]y = [6, 10, 4, 5, 1]labels = list('ABCDE') fig, axes = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(18, 20), facecolor='cornsilk')axes = axes.ravel()i = 0ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 )args = [[e] for e in ['x', 'height']]# 向图表子区添加标题和数据表title_table(ax)i = 1ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 )args = [[e] for e in ['x', 'height', 'align']]# 向图表子区添加标题和数据表title_table(ax)i = 2ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 color='c', # 柱体的填充颜色 )args = [[e] for e in ['x', 'height', 'align', 'color']]# 向图表子区添加标题和数据表title_table(ax)i = 3ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对齐的位置 color='cyan', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label']]# 向图表子区添加标题和数据表title_table(ax)i = 4ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 color='blue', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 alpha=0.6 # 柱体填充颜色的透明度 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha']]# 向图表子区添加标题和数据表title_table(ax)i = 5ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 color='wheat', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 alpha=0.6, # 柱体填充颜色的透明度 width=1, # 柱体的宽度 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width']]# 向图表子区添加标题和数据表title_table(ax)i = 6ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 color='aqua', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 alpha=0.6, # 柱体填充颜色的透明度 width=0.8, # 柱体的宽度 bottom=0.2, # 柱体基线的 y 轴坐标 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom']]# 向图表子区添加标题和数据表title_table(ax)i = 7ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='edge', # x 轴上的坐标与柱体对其的位置 color='lightpink', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 alpha=0.6, # 柱体填充颜色的透明度 width=0.8, # 柱体的宽度 bottom=0.2, # 柱体基线的 y 轴坐标 edgecolor='g' # 柱体的边框颜色 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom', 'edgecolor']]# 向图表子区添加标题和数据表title_table(ax)i = 8ax = axes[i]# 绘制柱状图ax.bar(x=x, # 柱体在 x 轴上的坐标位置 height=y, # 柱体的高度 align='center', # x 轴上的坐标与柱体对其的位置 color='bisque', # 柱体的填充颜色 tick_label=labels, # 每个柱体的标签名称 alpha=0.6, # 柱体填充颜色的透明度 width=0.8, # 柱体的宽度 bottom=0.2, # 柱体基线的 y 轴坐标 edgecolor='g', # 柱体的边框颜色 linewidth=1.5, # 柱体边框线的宽度 )args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom', 'edgecolor', 'linewidth']]# 向图表子区添加标题和数据表title_table(ax)# 设置整个子区的布局fig.subplots_adjust(left=0, bottom=0, right=0.9, top=1, wspace=0.1, # 子区间空白区域的宽度的归一化值 hspace=0.2); # 子区间空白区域的高度的归一化值 图表 
以上为个人经验,希望能给大家一个参考,也希望大家多多支持51zixue.net。 matlab xlabel位置的设置方式 python3 使用函数求两个数的和与差 |