python绘制柱状图,如何改变柱状柱间距,如何设置横纵轴标签(绘制Intel Realsense D435深度误差柱状图)
生活随笔
收集整理的這篇文章主要介紹了
python绘制柱状图,如何改变柱状柱间距,如何设置横纵轴标签(绘制Intel Realsense D435深度误差柱状图)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- [參考文章1:Python 繪制 柱狀圖](https://www.cnblogs.com/shenxiaolin/p/11100094.html)
- 我的代碼
參考文章1:Python 繪制 柱狀圖
# 創建一個點數為 8 x 6 的窗口, 并設置分辨率為 80像素/每英寸 plt.figure(figsize=(10, 10), dpi=80) # 再創建一個規格為 1 x 1 的子圖 # plt.subplot(1, 1, 1) # 柱子總數 N = 10 # 包含每個柱子對應值的序列 values = (56796,42996,24872,13849,8609,5331,1971,554,169,26) # 包含每個柱子下標的序列 index = np.arange(N) # 柱子的寬度 width = 0.45 # 繪制柱狀圖, 每根柱子的顏色為紫羅蘭色 p2 = plt.bar(index, values, width, label="num", color="#87CEFA") # 設置橫軸標簽 plt.xlabel('clusters') # 設置縱軸標簽 plt.ylabel('number of reviews') # 添加標題 plt.title('Cluster Distribution') # 添加縱橫軸的刻度 plt.xticks(index, ('mentioned1cluster', 'mentioned2cluster', 'mentioned3cluster', 'mentioned4cluster', 'mentioned5cluster', 'mentioned6cluster', 'mentioned7cluster', 'mentioned8cluster', 'mentioned9cluster', 'mentioned10cluster')) # plt.yticks(np.arange(0, 10000, 10)) # 添加圖例 plt.legend(loc="upper right") plt.show()我的代碼
# -*- coding: utf-8 -*- """ @File : 測試擬合平面.py @Time : 2020/9/8 9:23 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from numba import jit# 加載數據 def load_data(file_path):# 讀取數據npzfile = np.load(file_path)# x, y, z = npzfile['arr_0'], npzfile['arr_1'], npzfile['arr_2']return npzfile['x'], npzfile['y'], npzfile['z']# 計算擬合片面 def cal_flat(x_, y_, z_):# 取樣點數量point_num = len(x_)print('len={}'.format(point_num))# 創建系數矩陣Aa = 0A = np.ones((point_num, 3))for i in range(0, point_num):A[i, 0] = x_[a]A[i, 1] = y_[a]a = a + 1# print(A)# 創建矩陣bb = np.zeros((point_num, 1))a = 0for i in range(0, point_num):b[i, 0] = z_[a]a = a + 1# print(b)# 通過X=(AT*A)-1*AT*b直接求解A_T = A.TA1 = np.dot(A_T, A)A2 = np.linalg.inv(A1)A3 = np.dot(A2, A_T)X = np.dot(A3, b)print('平面擬合結果為:z = %.6f * x + %.6f * y + %.6f' % (X[0, 0], X[1, 0], X[2, 0]))# 計算方差R = 0for i in range(0, point_num):R = R + (X[0, 0] * x_[i] + X[1, 0] * y_[i] + X[2, 0] - z_[i]) ** 2print('方差為:%.*f' % (3, R))return X# 繪制三維深度散點圖及擬合平面 def show_flat(X):# 展示圖像fig1 = plt.figure()ax1 = fig1.add_subplot(111, projection='3d')ax1.set_xlabel("x")ax1.set_ylabel("y")ax1.set_zlabel("z")# ax1.scatter(x, y, z, c='r', marker='o')ax1.scatter(x, y, z, c='r', marker='.')x_p = np.linspace(-200, 1480, 168)y_p = np.linspace(-200, 920, 112)x_p, y_p = np.meshgrid(x_p, y_p)z_p = X[0, 0] * x_p + X[1, 0] * y_p + X[2, 0]ax1.plot_wireframe(x_p, y_p, z_p, rstride=10, cstride=10)plt.show()# 計算深度誤差 def cal_error_distribution(x_, y_, z_, coefficient_):# 計算誤差error = z_ - (coefficient_[0, 0] * x_ + coefficient_[1, 0] * y_ + coefficient_[2, 0])return error# 統計各個區間誤差的數量 def count_error_number(depth_error_, column_num):# 提取負向最大深度誤差和正向最大深度誤差error_max = np.max(depth_error)# print(error_max) # 23.39967553034205error_min = np.min(depth_error)# print(error_min) # -18.65760653439031# 柱子寬度error_width = (error_max - error_min) / column_num# print(column_width) # 1.6822912825892944# 創建柱子高度序列column_height_sequence = np.zeros(column_num)# 統計每個柱子包含深度點的數量for error in depth_error_:index = int((error - error_min) // error_width)# 當深度誤差error等于最大深度誤差的時候,下標會溢出,所以要加個判斷限制一下if index == column_num:# print(dep) # 23.39967553034205index -= 1column_height_sequence[index] += 1# print(column_height_sequence)return column_height_sequence# 繪制柱狀圖 def draw_histogram(column_height_sequence_):# 創建一個點數為 8 x 6 的窗口, 并設置分辨率為 80像素/每英寸plt.figure(figsize=(10, 10), dpi=80)# plt.figure()# 再創建一個規格為 1 x 1 的子圖# plt.subplot(1, 1, 1)# 柱子總數N = len(column_height_sequence)# 包含每個柱子對應值的序列values = column_height_sequence_# 包含每個柱子下標的序列index = np.arange(N)# 柱子的寬度width = 1# 繪制柱狀圖, 每根柱子的顏色為紫羅蘭色# p2 = plt.bar(index, values, width, label="num", color="#87CEFA")p2 = plt.bar(index, values, width, label="num", color="#87CEFA")# 設置橫軸標簽plt.xlabel('error/mm')# 設置縱軸標簽plt.ylabel('number of points')# 添加標題plt.title('D435 error Distribution(500mm)')# 添加縱橫軸的刻度index_new = []for index_ in index:if index_ % 100 == 0:index_new.append(index_)# print(index_new)index_new_content = []for index in index_new:error_max = np.max(depth_error)error_min = np.min(depth_error)index_new_content.append(error_min + (error_max - error_min) * index / N)index_new_content_string = [str(i) for i in np.around(index_new_content, decimals=1)]# plt.xticks(index, (# 'mentioned1cluster', 'mentioned2cluster', 'mentioned3cluster', 'mentioned4cluster', 'mentioned5cluster',# 'mentioned6cluster', 'mentioned7cluster', 'mentioned8cluster', 'mentioned9cluster', 'mentioned10cluster'))plt.xticks(index_new, index_new_content_string)# plt.yticks(np.arange(0, 10000, 10))# 添加圖例plt.legend(loc="upper right")plt.show()if __name__ == '__main__':# 加載數據x, y, z = load_data('./data/500mm/4_1599623292.4111953.npz')# 擬合平面并返回平面函數系數coefficient = cal_flat(x, y, z)# 繪制平面及深度散點# show_flat(coefficient)# 計算深度誤差depth_error = cal_error_distribution(x, y, z, coefficient)# 統計各個誤差區間的數量(柱狀圖各個柱子高度)column_height_sequence = count_error_number(depth_error, 1000)# 繪制柱狀圖draw_histogram(column_height_sequence)
代碼中深度數據:
https://download.csdn.net/download/Dontla/12833241
總結
以上是生活随笔為你收集整理的python绘制柱状图,如何改变柱状柱间距,如何设置横纵轴标签(绘制Intel Realsense D435深度误差柱状图)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python如何拟合三维平面(拟合Int
- 下一篇: gifcam录制动态图后导入photos