欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

效果圖展示如下:

該代碼可以處理多個(gè)實(shí)驗(yàn)多組觀測(cè)值的展示,代碼如下:

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:

? ? """

? ? # 用于正常顯示中文標(biāo)簽
? ? # 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) ?# 假如設(shè)置為221,則表示創(chuàng)建兩行兩列也就是4個(gè)子畫板,ax為第一個(gè)子畫板

? ? # 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

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

? ? # 設(shè)置y軸的刻標(biāo)注
? ? 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)
? ? # 把軸的刻度間隔設(shè)置為1,并存在變量里
? ? y_major_locator = MultipleLocator(2.5)
? ? ax.yaxis.set_major_locator(y_major_locator)

? ? # 設(shè)置標(biāo)題
? ? 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)
? ? # 設(shè)置邊框線寬為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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Python隨機(jī)生成數(shù)據(jù)的方法

    使用Python隨機(jī)生成數(shù)據(jù)的方法

    這篇文章主要介紹了使用Python隨機(jī)生成數(shù)據(jù)的方法,在日常開(kāi)發(fā)中竟然會(huì)遇到需要測(cè)試大量數(shù)據(jù)的地方,那么隨機(jī)生成數(shù)據(jù)就可以有效的加快我們的效率,通過(guò)Python_Faker生成測(cè)試數(shù)據(jù)需要安裝Faker包,需要的朋友可以參考下
    2023-10-10
  • pytorch中retain_graph==True的作用說(shuō)明

    pytorch中retain_graph==True的作用說(shuō)明

    這篇文章主要介紹了pytorch中retain_graph==True的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 什么是python的id函數(shù)

    什么是python的id函數(shù)

    在本篇文章里小編給大家分享了關(guān)于python里id函數(shù)的基礎(chǔ)知識(shí)點(diǎn),需要的朋友們可以一起學(xué)習(xí)下。
    2020-06-06
  • python實(shí)現(xiàn)簡(jiǎn)易淘寶購(gòu)物

    python實(shí)現(xiàn)簡(jiǎn)易淘寶購(gòu)物

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易淘寶購(gòu)物,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python新手必讀bytearray對(duì)象使用技巧掌握

    Python新手必讀bytearray對(duì)象使用技巧掌握

    Python中的bytearray是一個(gè)可變序列,通常用于存儲(chǔ)二進(jìn)制數(shù)據(jù),它允許在不創(chuàng)建新的對(duì)象的情況下就地修改數(shù)據(jù),非常適用于處理字節(jié)數(shù)據(jù),本文將深入學(xué)習(xí)bytearray對(duì)象的使用,包括創(chuàng)建、修改、切片和常見(jiàn)應(yīng)用場(chǎng)景
    2023-12-12
  • Python繪制驚艷的可視化動(dòng)圖的示例代碼

    Python繪制驚艷的可視化動(dòng)圖的示例代碼

    今天小編給大家介紹一款可視化模塊,使用它可以繪制出十分驚艷的動(dòng)圖效果。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-04-04
  • 利用Python實(shí)現(xiàn)顏色色值轉(zhuǎn)換的小工具

    利用Python實(shí)現(xiàn)顏色色值轉(zhuǎn)換的小工具

    最近一個(gè)朋友說(shuō)已經(jīng)轉(zhuǎn)用Zeplin很久了。Zeplin的設(shè)計(jì)稿展示頁(yè)面的顏色色值使用十進(jìn)制的 RGB 表示的,在 Android 中的顏色表示大多情況下都需要十六進(jìn)制的 RGB 表示。所以想寫個(gè)工作,當(dāng)輸入十進(jìn)制的RGB ,得到十六進(jìn)制的色值,最好可以方便復(fù)制。下面來(lái)一起看看吧。
    2016-10-10
  • Python中實(shí)現(xiàn)最小二乘法思路及實(shí)現(xiàn)代碼

    Python中實(shí)現(xiàn)最小二乘法思路及實(shí)現(xiàn)代碼

    這篇文章主要介紹了Python中實(shí)現(xiàn)最小二乘法思路及實(shí)現(xiàn)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • python實(shí)現(xiàn)sublime3的less編譯插件示例

    python實(shí)現(xiàn)sublime3的less編譯插件示例

    這篇文章主要介紹了python實(shí)現(xiàn)sublime3的less編譯插件示例的相關(guān)資料
    2014-04-04
  • Python中的元類編程入門指引

    Python中的元類編程入門指引

    這篇文章主要介紹了Python中的元類編程入門指引,來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04

最新評(píng)論