日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python 柱状图宽度设置_Python matplotlib 柱状图实例

發(fā)布時(shí)間:2025/4/5 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 柱状图宽度设置_Python matplotlib 柱状图实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(xué)習(xí)用matplotlib繪圖中,數(shù)據(jù)是我之前做的實(shí)驗(yàn),隱去了關(guān)鍵信息。

出來的效果就是題圖這樣,基本可以滿足柱狀圖的繪圖要求。

完整代碼及注釋如下:

import numpy as np

import matplotlib.pyplot as plt

#解決顯示不出中文的問題

from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei'] #指定默認(rèn)字體

mpl.rcParams['axes.unicode_minus'] = False #解決保存圖像是負(fù)號'-'顯示為方塊的問題

n_groups = 3 #幾組數(shù)據(jù)

#三組數(shù)據(jù)中每組2個(gè)數(shù)據(jù)

means_men = (433.147, 452.569,444.969 )

std_men = (42.606, 39.889, 45.779)

means_women = (503.508, 552.544,539.922)

std_women = (43.549,53.917,43.131)

fig, ax = plt.subplots() #這個(gè)設(shè)置用于后面在每個(gè)主題上顯示數(shù)值

#設(shè)置索引和柱體寬度

index = np.arange(n_groups)

bar_width = 0.4

#不透明度和error bar的顏色

opacity = 0.4

error_config = {'ecolor': '0.3'}

#每組數(shù)據(jù)的設(shè)置

rects1 = plt.bar(index, means_men, bar_width,

alpha=opacity, #alpha :float (0.0 transparent through 1.0 opaque)

color='b',

yerr=std_men,

error_kw=error_config, #error bar

label='男') #每組數(shù)據(jù)中每個(gè)數(shù)據(jù)的標(biāo)簽

rects2 = plt.bar(index + bar_width, means_women, bar_width, #注意 index+bar_width

alpha=opacity,

color='r',

yerr=std_women,

error_kw=error_config,

label='女')

#設(shè)置柱體上顯示數(shù)據(jù)值

def autolabel(rects):

# attach some text labels

for rect in rects:

height = rect.get_height()

ax.text(rect.get_x()+rect.get_width()/2.0, 1.1*height,

'%.2f'%float(height), ha='center', va='bottom')

#‘%.2f’%float(height)這個(gè)設(shè)置是讓顯示的數(shù)值精度為小數(shù)點(diǎn)后兩位小數(shù)

autolabel(rects1)

autolabel(rects2)

plt.xlabel('x軸')

plt.ylabel('y軸')

plt.title('圖表標(biāo)題')

plt.xticks(index + bar_width / 2, ("a組", 'b組', 'c組'))

plt.ylim(0,1000) #設(shè)置y軸的標(biāo)尺

plt.xlim(-0.5,3) #設(shè)置x軸的標(biāo)尺

plt.legend()

fig.savefig('圖片名稱.png',dpi=600) #保存圖片并設(shè)置分辨率

基本上是從網(wǎng)上按需求查找并整理出來的,作為記錄使用。

總結(jié)

以上是生活随笔為你收集整理的python 柱状图宽度设置_Python matplotlib 柱状图实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。