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 plt2 繪圖,逐步設置 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); # 子區間空白區域的高度的歸一化值
圖表
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: