python 如何設(shè)置柱狀圖參數(shù)
version:python 3.6
環(huán)境:anaconda/JupyterLab 0.27.0
操作系統(tǒng):Windows 10
import pandas as pd import matplotlib.pyplot as plt a = 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()
補(bǔ)充:python 用 matplotlib 繪制柱狀圖參數(shù)詳解 plt.bar()
1、加載庫
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt
2 繪圖,逐步設(shè)置 bar() 參數(shù)
def title_table(ax): '''為圖表添加標(biāo)題和表格''' ax.set_title(label=f'No.{i+1}', loc='center', pad=None, fontdict={'color': 'b'} ) ax.table(loc='upper right', # 表格在圖表區(qū)的位置 colLabels=[f'{i+2} args'], # 表格每列的列名稱 colColours=['g'], # 表格每列列名稱所在單元格的填充顏色 colLoc='left', # 表格中每列列名稱的對齊位置 colWidths=[0.15], # 表格每列的寬度 cellText=args, # 表格中的數(shù)值, 每行數(shù)據(jù)的列表的列表 cellColours=[['cornsilk']]*len(args), # 表格中數(shù)據(jù)所在單元格的填充顏色 cellLoc='left', # 表格中數(shù)據(jù)的對齊位置 fontsize=8) # 配置字體,顯示中文 mpl.rcParams['font.sans-serif'] = ['SimHei'] # 配置坐標(biāo)軸刻度值模式,顯示負(fù)號 mpl.rcParams['axes.unicode_minus'] = True # 定義數(shù)據(jù) 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 = 0 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 ) args = [[e] for e in ['x', 'height']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 1 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 ) args = [[e] for e in ['x', 'height', 'align']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 2 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 color='c', # 柱體的填充顏色 ) args = [[e] for e in ['x', 'height', 'align', 'color']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 3 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對齊的位置 color='cyan', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 4 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 color='blue', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 alpha=0.6 # 柱體填充顏色的透明度 ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 5 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 color='wheat', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 alpha=0.6, # 柱體填充顏色的透明度 width=1, # 柱體的寬度 ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 6 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 color='aqua', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 alpha=0.6, # 柱體填充顏色的透明度 width=0.8, # 柱體的寬度 bottom=0.2, # 柱體基線的 y 軸坐標(biāo) ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 7 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='edge', # x 軸上的坐標(biāo)與柱體對其的位置 color='lightpink', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 alpha=0.6, # 柱體填充顏色的透明度 width=0.8, # 柱體的寬度 bottom=0.2, # 柱體基線的 y 軸坐標(biāo) edgecolor='g' # 柱體的邊框顏色 ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom', 'edgecolor']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) i = 8 ax = axes[i] # 繪制柱狀圖 ax.bar(x=x, # 柱體在 x 軸上的坐標(biāo)位置 height=y, # 柱體的高度 align='center', # x 軸上的坐標(biāo)與柱體對其的位置 color='bisque', # 柱體的填充顏色 tick_label=labels, # 每個(gè)柱體的標(biāo)簽名稱 alpha=0.6, # 柱體填充顏色的透明度 width=0.8, # 柱體的寬度 bottom=0.2, # 柱體基線的 y 軸坐標(biāo) edgecolor='g', # 柱體的邊框顏色 linewidth=1.5, # 柱體邊框線的寬度 ) args = [[e] for e in ['x', 'height', 'align', 'color', 'tick_label', 'alpha', 'width', 'bottom', 'edgecolor', 'linewidth']] # 向圖表子區(qū)添加標(biāo)題和數(shù)據(jù)表 title_table(ax) # 設(shè)置整個(gè)子區(qū)的布局 fig.subplots_adjust(left=0, bottom=0, right=0.9, top=1, wspace=0.1, # 子區(qū)間空白區(qū)域的寬度的歸一化值 hspace=0.2); # 子區(qū)間空白區(qū)域的高度的歸一化值
圖表
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Caffe數(shù)據(jù)可視化環(huán)境python接口配置教程示例
這篇文章主要為大家介紹了Caffe數(shù)據(jù)可視化環(huán)境python接口配置教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python嵌套函數(shù)與nonlocal使用詳細(xì)介紹
這篇文章主要介紹了Python嵌套函數(shù)與nonlocal使用,nonlocal關(guān)鍵字與global關(guān)鍵字有點(diǎn)相似,可以對比著理解。nonlocal關(guān)鍵字只能作用域局部變量,且始終找離當(dāng)前最近的上層局部作用域中的變量2022-09-09Python批量刪除或移動(dòng)指定圖像的實(shí)現(xiàn)示例
本文主要介紹了Python批量刪除或移動(dòng)指定圖像,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python中TCP協(xié)議的探索與實(shí)例解析
網(wǎng)絡(luò)編程在當(dāng)今數(shù)字化世界中扮演著至關(guān)重要的角色,本文將帶你深入了解 Python 中的 TCP 協(xié)議,介紹網(wǎng)絡(luò)編程的基礎(chǔ)知識(shí),并提供豐富的示例代碼,希望對大家有所幫助2023-12-12Python隨機(jī)讀取文件實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Python隨機(jī)讀取文件的相關(guān)資料,需要的朋友可以參考下2017-05-05關(guān)于Python ImportError: No module named&nb
最近多個(gè)小伙伴兒問“ImportError: No module named xxx“,應(yīng)該怎么樣解決,下面小編給大家?guī)砹岁P(guān)于Python ImportError: No module named 通用解決方法,感興趣的朋友一起看看吧2022-11-11python打包生成的exe文件運(yùn)行時(shí)提示缺少模塊的解決方法
今天小編就為大家分享一篇python打包生成的exe文件運(yùn)行時(shí)提示缺少模塊的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10