日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

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

學習用matplotlib繪圖中,數據是我之前做的實驗,隱去了關鍵信息。

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

完整代碼及注釋如下:

import numpy as np

import matplotlib.pyplot as plt

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

from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei'] #指定默認字體

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

n_groups = 3 #幾組數據

#三組數據中每組2個數據

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() #這個設置用于后面在每個主題上顯示數值

#設置索引和柱體寬度

index = np.arange(n_groups)

bar_width = 0.4

#不透明度和error bar的顏色

opacity = 0.4

error_config = {'ecolor': '0.3'}

#每組數據的設置

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='男') #每組數據中每個數據的標簽

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='女')

#設置柱體上顯示數據值

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)這個設置是讓顯示的數值精度為小數點后兩位小數

autolabel(rects1)

autolabel(rects2)

plt.xlabel('x軸')

plt.ylabel('y軸')

plt.title('圖表標題')

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

plt.ylim(0,1000) #設置y軸的標尺

plt.xlim(-0.5,3) #設置x軸的標尺

plt.legend()

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

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。