1. 程式人生 > 其它 >python畫統計圖(二)

python畫統計圖(二)

python畫統計圖(二)

fig = plt.gca()	# 獲取當前的Figure物件,如果沒有就新建一個Figure

# 儲存統計圖
fig.savefig('figure.pdf', dpi=fig.dpi, format='pdf', pad_inches=0,bbox_inches='tight')

api路徑:matplotlib.figure.Figure.savefig(self, fname, *, transparent=None, **kwargs)

bar()

import matplotlib.pyplot as plt

plt.ion()	# 互動模式

fig, ax = plt.subplots()	# 建立畫布

ax.set_title('Simple bar')	# 設定標題

# 建立條形圖
label1 = ax.bar(
    x = [1, 1.6, 2.2],
    height = [2, 3, 5],
    width = 0.2,
    bottom = 0.5,
    align = 'edge',
)
label1.set_label('label1')

label2 = ax.bar(
    x = [1.2, 1.8, 2.4],
    height = [2.5, 3.1, 5.4],
    width = 0.2,
    bottom = 0.5,
    align = 'edge',
)
label2.set_label('label2')

ax.bar(
    x = [1.2, 1.8, 2.4],
    height = [0, 0, 0],
    width = [0, 0, 0],
    align = 'center',
    tick_label = ['5', '10', '15']
)

ax.legend([label1, label2], ['label1', 'label2'])	# 自動建立圖例

ax.clear()	# 清除所有