1.畫兩個條形圖,bar和barh, 同時axes[0].axhline畫一條橫線,axes[1].axvline畫一條豎線
import numpy as np import matplotlib.pyplot as plt
np.random.seed(0) x = np.arange(5) y = np.random.randint(-5, 5, 5) # 分成兩個方框,一行兩列 fig,axes = plt.subplots(ncols=2) # 對第一個方框進行畫圖bar axes[0].bar(x, y, color='r') # 對第二個方框進行畫圖barh axes[1].barh(x, y, color='g') # 對第一個方框畫一條橫線,axhline axes[0].axhline(0, color='grey', linewidth=2) # 對第二個方框畫一條豎線, axvline axes[1].axvline(0, color='grey', linewidth=2) plt.show()
2.根據條形圖y的大小設置每個條形圖的顏色
np.random.seed(0) fig, ax = plt.subplots() v_bars = ax.bar(x, y, color='lightblue') for bars, height in zip(v_bars, y): if height < 0: bars.set(edgecolor='darkred', color='green', linewidth=2) plt.show()
3.fill_between 進行填充操作, .consum 表示的是進行累加操作,將前一個值累加到當前值
x = np.random.randn(100).cumsum() y = np.linspace(0, 100, 100) fig, ax = plt.subplots() ax.fill_between(x, y, color='lightblue') plt.show()
4. fill_between 對兩個函數之間進行填充操作
x = np.linspace(0, 10, 200) y1 = 2*x + 1 y2 = 3*x + 1.2 y_mean = 0.5*x*np.cos(2*x) + 2.5*x + 1.1 fig, ax = plt.subplots()
# 填充函數之間的值 ax.fill_between(x, y1, y2, color='red') plt.plot(x, y_mean, color='black') plt.show()
5. 誤差棒的設置plt.bar(yerr=variance) # variance表示誤差的大小
mean_values = [1, 2, 3] # variance表示誤差 variance = [0.2, 0.4, 0.5] bar_label = ['bar1', 'bar2', 'bar3'] # 設置x軸的范圍 x_pos = list(range(len(mean_values))) # yerr用來設置誤差棒 plt.bar(x_pos, mean_values, color='r', yerr=variance) max_y = max(zip(mean_values, variance)) # 設置y軸的顯示范圍 plt.ylim([0, (max_y[0] + max_y[1]) * 1.2]) # 設置x軸的標簽,x_pos表示標簽的位置,bar_label表示標簽名 plt.xticks(x_pos, bar_label) plt.ylabel('variable_y') plt.show()
6. 畫橫向並排的條形圖
x1 = np.array([1, 2, 3]) x2 = np.array([1, 2, 3]) bar_label = ['bar1', 'bar2', 'bar3'] fig = plt.figure(figsize=[8, 6]) y_pos = list(np.arange(len(x1))) plt.barh(y_pos, x1, color='r',) plt.barh(y_pos, -x2, color='b',) # 為了使得圖形美觀,設置xlim和ylim的范圍 plt.xlim([-max(x2)-1, max(x1) + 1]) plt.ylim(-1, max(y_pos) + 1) plt.show()
7.設置位置的寬度進行並排
green_data = [1, 2, 3] blue_data = [3, 2, 1] red_data = [2, 3, 3] labels = ['group1', 'group2', 'group3'] # 設置x_pos的位置 x_pos = list(range(len(green_data))) print(x_pos) width = 0.2 plt.bar(x_pos, green_data, width=width, color='g', label=labels[0], alpha=0.6) plt.bar([x+width for x in x_pos], blue_data, width=width, color='b', label=labels[1], alpha=0.6) plt.bar([x+2*width for x in x_pos], red_data, width=width, color='r', label=labels[2], alpha=0.6) plt.legend(loc='best', framealpha=0.05) plt.show()
8. 在畫的條形圖上加豎線ax.axvline(data.min()) 在條形圖的后面加上%plt.text 通過獲得每個條形圖的位置信息
data = np.arange(200, 225, 5) bar_labels = ['a', 'b', 'c', 'd', 'e'] y_pos = list(np.arange(len(data))) fig, ax = plt.subplots() # ax.spines['top'].set_visible(False) # 去除右邊的框圖 ax.spines['right'].set_visible(False) plt.yticks(y_pos, bar_labels) v_bars = plt.barh(y_pos, data) # 加橫線 ax.axvline(data.min(), linestyle='--', color='k') # 進行對每一個條形塊文本標注 for b, d in zip(v_bars, data): plt.text(b.get_width(), b.get_y()+b.get_height()/2, '{0:.2%}'.format(d/min(data))) plt.show()
9. 構造colormap來建立條形圖的顏色 使用colormap構造條形圖cm.ScalarMappable(col.Normalize())
import matplotlib.colors as col import matplotlib.cm as cm mean_values = range(10, 18) x_pos = range(len(mean_values)) # 構造colormap, cm.hot表示風格 cmap1 = cm.ScalarMappable(col.Normalize(min(mean_values), max(mean_values), cm.hot)) cmap2 = cm.ScalarMappable(col.Normalize(0, 20, cm.hot)) plt.subplot(121) # cmap1.to_rgba(mean_values) # 將color_map使用到mean_values plt.bar(x_pos, mean_values, color=cmap1.to_rgba(mean_values)) plt.subplot(122) plt.bar(x_pos, mean_values, color=cmap2.to_rgba(mean_values)) plt.show()
10. b.set_hatch('o') # 畫出不同填充風格的條形圖
patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.') data = range(1, len(patterns) + 1 ) x_pow = range(len(data)) bars = plt.bar(x_pow, data) for b, p in zip(bars, patterns): b.set_hatch(p) plt.show()