python 柱状图 居中_python matplotlib模块: bar(柱状图)
plt模塊的bar方法可以幫助我們繪制豎著的柱狀圖。
功能非常強(qiáng)大, 使用起來非常簡單, 詳細(xì)注釋放在源碼。
其中各種顏色的hex值可以從:
各種顏色hex值獲取
源碼:
# coding=utf-8
from matplotlib import pyplot as plt
import numpy as np
plt.style.use('fivethirtyeight')
# 可以查看所有可供使用的展示風(fēng)格
# print(plt.style.available)
dev_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
# 創(chuàng)建一維的等差數(shù)列, 是一個ndarray對象, 類似于[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ....]
x_indexes = np.arange(len(dev_x))
dev_y = [38496, 42000, 46752, 49320, 53200,
56000, 62316, 64928, 67317, 68748, 73752]
py_dev_y = [45372, 48876, 53850, 57287, 63016,
65998, 70003, 70000, 71496, 75370, 83640]
js_dev_y = [37810, 43515, 46823, 49293, 53437,
56373, 62375, 66674, 68745, 68746, 74583]
# 設(shè)置每個柱狀體(bar)的寬度
width = 0.25
# 這個數(shù)據(jù)集設(shè)置的柱狀體顏色是綠色, 放置在x軸的0-0.25, 1-0.25, 2-0.25, ...位置上(也就是在中間柱體緊鄰的左邊), 寬度0.25
plt.bar(x_indexes - width, dev_y, width=width,
color='#18F904', label='all_devs')
# 這個數(shù)據(jù)集設(shè)置的柱狀體顏色是紅色, 放置在x軸的0, 1, 2, 3, 4, 5, ...位置上, 寬度0.25
plt.bar(x_indexes, py_dev_y, width=width, color='#F90404', label='Python')
# 這個數(shù)據(jù)集設(shè)置的柱狀體顏色是藍(lán)色, 放置在x軸的0+0.25, 1+0.25, 2+0.25, ...位置上(也就是在中間柱體緊鄰的右邊), 寬度0.25
plt.bar(x_indexes + width, js_dev_y, width=width,
color='#0451F9', label='JavaScript')
# 將柱狀體的x軸原本的下標(biāo)映射到新的標(biāo)簽上
plt.xticks(ticks=x_indexes, labels=dev_x)
# 設(shè)置標(biāo)題, x軸的標(biāo)簽, y軸的標(biāo)簽
plt.title('First bar')
plt.xlabel('Developer ages')
plt.ylabel("Developer's annual salary")
# 設(shè)置每一組數(shù)據(jù)展示圖象的標(biāo)簽(用以區(qū)分不同的數(shù)據(jù)圖象), 不加legend方法, 前面就算設(shè)置了lebel=XXX也沒用
plt.legend()
# 緊致最后的圖形, 更美觀
plt.tight_layout()
plt.show()
運行結(jié)果:
圖片.png
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python 柱状图 居中_python matplotlib模块: bar(柱状图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python idle在哪_python
- 下一篇: python中用于绘制各种图形、标注文本