最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python matplotlib實現(xiàn)條形統(tǒng)計圖

 更新時間:2022年04月21日 10:40:57   作者:quintus0505  
這篇文章主要為大家詳細介紹了Python matplotlib實現(xiàn)條形統(tǒng)計圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Python-matplotlib實現(xiàn)條形統(tǒng)計圖,供大家參考,具體內(nèi)容如下

效果圖展示如下:

該代碼可以處理多個實驗多組觀測值的展示,代碼如下:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import MultipleLocator

def plot_bar(experiment_name, bar_name, bar_value, error_value=None,):
? ? """

? ? Args:
? ? ? ? experiment_name: x_labels
? ? ? ? bar_name: legend name
? ? ? ? bar_value: list(len(experiment_name), each element contains a np.array(),
? ? ? ? ? ? ? ? ? ?which contains bar value in each group
? ? ? ? error_value: list(len(experiment_name), each element contains a np.array(),
? ? ? ? ? ? ? ? ? ?which contains error value in each group
? ? Returns:

? ? """

? ? # 用于正常顯示中文標簽
? ? # plt.rcParams["font.sans-serif"]=['SimHei']

? ? colors = ['lightsteelblue', 'cornflowerblue', 'royalblue', 'blue', 'mediumblue', 'darkblue', 'navy', 'midnightblue',
? ? ? ? ? ? ? 'lavender', ]

? ? assert len(bar_value[0]) <= len(colors) ?# if not try to add color to 'colors'

? ? plt.rcParams['axes.unicode_minus'] = False
? ? plt.style.use('seaborn')
? ? font = {'weight': 'normal', 'size': 20, }
? ? font_title = {'weight': 'normal', 'size': 28, }
? ? # bar width
? ? width = 0.2
? ? # groups of data
? ? x_bar = np.arange(len(experiment_name))
? ? # create figure
? ? plt.figure(figsize=(10, 9))

? ? ax = plt.subplot(111) ?# 假如設置為221,則表示創(chuàng)建兩行兩列也就是4個子畫板,ax為第一個子畫板

? ? # plot bar

? ? bar_groups = []
? ? value = []
? ? for i in range(len(bar_value[0])):
? ? ? ? for j in range(len(experiment_name)):
? ? ? ? ? ? value.append(bar_value[j][i])
? ? ? ? group = ax.bar(x_bar - (len(experiment_name)-3-i)*width, copy.deepcopy(value), width=width, color=colors[i], label=bar_name[i])
? ? ? ? bar_groups.append(group)
? ? ? ? value.clear()


? ? # add height to each bar
? ? i = j = 0
? ? for bars in bar_groups:
? ? ? ? j = 0
? ? ? ? for rect in bars:
? ? ? ? ? ? x = rect.get_x()
? ? ? ? ? ? height = rect.get_height()
? ? ? ? ? ? # ax.text(x + 0.1, 1.02 * height, str(height), fontdict=font)
? ? ? ? ? ? # error bar
? ? ? ? ? ? if error_value:
? ? ? ? ? ? ? ? ax.errorbar(x + width / 2, height, yerr=error_value[j][i], fmt="-", ecolor="black",
? ? ? ? ? ? ? ? ? ? ? ? ? ? elinewidth=1.2, capsize=2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? capthick=1.2)
? ? ? ? ? ? j += 1
? ? ? ? i += 1

? ? # 設置刻度字體大小
? ? plt.xticks(fontsize=15)
? ? plt.yticks(fontsize=18)
? ? # 設置x軸的刻度
? ? ax.set_xticks(x_bar)
? ? ax.set_xticklabels(experiment_name, fontdict=font)

? ? # 設置y軸的刻標注
? ? ax.set_ylabel("Episode Cost", fontdict=font_title)
? ? ax.set_xlabel('Experiment', fontdict=font_title)

? ? # 是否顯示網(wǎng)格
? ? ax.grid(False)

? ? # 拉伸y軸
? ? ax.set_ylim(0, 7.5)
? ? # 把軸的刻度間隔設置為1,并存在變量里
? ? y_major_locator = MultipleLocator(2.5)
? ? ax.yaxis.set_major_locator(y_major_locator)

? ? # 設置標題
? ? plt.suptitle("Cost Comparison", fontsize=30, horizontalalignment='center')

? ? plt.subplots_adjust(left=0.11, bottom=0.1, right=0.95, top=0.93, wspace=0.1, hspace=0.2)
? ? # 設置邊框線寬為2.0
? ? ax.spines['bottom'].set_linewidth('2.0')
? ? # 添加圖例
? ? ax.legend(loc='upper left', frameon=True, fontsize=19.5)
? ? # plt.savefig("test.png")
? ? plt.show()
? ? plt.legend()

if __name__ == "__main__":
? ? test_experiment_name = ["Test 1", "Test 2", "Test 3", "Test 4"]
? ? test_bar_name = ['A', "B", "C"]
? ? test_bar_value = [
? ? ? ? np.array([1, 2, 3]),
? ? ? ? np.array([4, 5, 6]),
? ? ? ? np.array([3, 2, 4]),
? ? ? ? np.array([5, 2, 2])
? ? ]
? ? test_error_value = [
? ? ? ? np.array([1, 1, 2]),
? ? ? ? np.array([0.2, 0.6, 1]),
? ? ? ? np.array([0, 0, 0]),
? ? ? ? np.array([0.5, 0.2, 0.2])
? ? ]
? ? plot_bar(test_experiment_name, test_bar_name, test_bar_value, test_error_value)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 在 Python 中接管鍵盤中斷信號的實現(xiàn)方法

    在 Python 中接管鍵盤中斷信號的實現(xiàn)方法

    要使用信號,我們需用導入 Python 的signal庫。然后自定義一個信號回調(diào)函數(shù),當 Python 收到某個信號時,調(diào)用這個函數(shù)。 ,下面通過實例代碼給大家介紹在 Python 中接管鍵盤中斷信號,需要的朋友可以參考下
    2020-02-02
  • python3?http.client?網(wǎng)絡請求方式

    python3?http.client?網(wǎng)絡請求方式

    這篇文章主要介紹了python3?http.client?網(wǎng)絡請求方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python使用matplotlib繪制三維圖形示例

    Python使用matplotlib繪制三維圖形示例

    這篇文章主要介紹了Python使用matplotlib繪制三維圖形,結(jié)合實例形式分析了Python基于matplotlib庫繪制三維圖形的相關操作技巧與注意事項,需要的朋友可以參考下
    2018-08-08
  • python+selenium實現(xiàn)QQ郵箱自動發(fā)送功能

    python+selenium實現(xiàn)QQ郵箱自動發(fā)送功能

    這篇文章主要為大家詳細介紹了python+selenium實現(xiàn)QQ郵箱自動發(fā)送功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回

    Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回

    這篇文章主為大家要介紹了Python3.10接入ChatGPT實現(xiàn)逐句回答流式返回示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作

    python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作

    這篇文章主要介紹了python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 詳解Python中文分詞而生的jieba庫

    詳解Python中文分詞而生的jieba庫

    這篇文章主要介紹了詳解Python中文分詞而生的jieba庫,在Python中,最好用的中文分詞庫是jieba。用“結(jié)巴”給一個中文分詞庫命名,非常生動形象,同時還帶有一種程序員式的幽默感,需要的朋友可以參考下
    2023-07-07
  • python實現(xiàn)簡單五子棋小游戲

    python實現(xiàn)簡單五子棋小游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單五子棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Python使用pip安裝Matplotlib的方法詳解

    Python使用pip安裝Matplotlib的方法詳解

    在網(wǎng)上看見許多matplotlib的安裝教程都是比較復雜,需要配置許多環(huán)境,對于電腦基礎不好的人來說可是一件頭疼的事情,今天我介紹一個簡單的安裝方法,下面這篇文章主要給大家介紹了關于Python使用pip安裝Matplotlib的相關資料,需要的朋友可以參考下
    2022-07-07
  • PyTorch實現(xiàn)卷積神經(jīng)網(wǎng)絡的搭建詳解

    PyTorch實現(xiàn)卷積神經(jīng)網(wǎng)絡的搭建詳解

    這篇文章主要為大家介紹了PyTorch實現(xiàn)卷積神經(jīng)網(wǎng)絡的搭建詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05

最新評論

安远县| 五峰| 乌鲁木齐市| 长子县| 米林县| 怀远县| 灯塔市| 五河县| 双辽市| 原平市| 左云县| 安西县| 海城市| 西盟| 广州市| 卫辉市| 长葛市| 鄂伦春自治旗| 香格里拉县| 陇南市| 大兴区| 顺义区| 景洪市| 略阳县| 安化县| 体育| 甘肃省| 津市市| 乐至县| 揭东县| 方城县| 长泰县| 福贡县| 绥化市| 林州市| 青龙| 陕西省| 县级市| 宽甸| 瓦房店市| 门源|