Install
sudo apt-get install python3-pandas
sudo apt-get install -y python3-seaborn
Note: Version command
-
Dependencies
Python 3.6+
-
Mandatory dependencies
numpy
(>= 1.13.3)
scipy
(>= 1.0.1)
pandas
(>= 0.22.0)
matplotlib
(>= 2.1.2)
Use
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
def plot_heatmap(robot_list, cp_list, mask = None):
x_label = []
for rob in range(0, 4):
for i in range(1,8):
x_label.append(robot_list[rob]+'_'+str(i))
f, ax= plt.subplots(figsize = (30, 8))
plt.subplots_adjust(left=0.05, right=0.95, top=0.98, bottom=0.05)
if mask is None:
ax = sns.heatmap(cp_list, annot=True, fmt='.3g', linewidths=.01,
annot_kws={'size':15}, yticklabels=robot_list, xticklabels=x_label,
cmap="YlGnBu", ax=ax, cbar=True,
square=False)
else:
with sns.axes_style("white"):
cbar_ax = f.add_axes([.65, .83, .3, .06])
ax = sns.heatmap(cp_list, annot=True, fmt='.3g', linewidths=.01, mask=mask,
annot_kws={'size':15}, yticklabels=robot_list, xticklabels=x_label,
cmap="YlGnBu", ax=ax, cbar_ax=cbar_ax, cbar=True,
cbar_kws={"orientation":"horizontal","shrink":1},
square=False)
# color: https://matplotlib.org/users/colormaps.html
# title
ax.set_title('Comparsion among four Franka robots', fontsize=24, position=(0.5, 1))
plt.subplots_adjust(top=0.92)
ax.set_xlabel('Joint of four robots',fontsize=20)
ax.set_ylabel('Robots',fontsize=20)
# matplotlib.axes.Axes.tick_params
# ax.tick_params(axis='y',labelsize=15) # y axis
# ax.tick_params(axis='x',labelsize=15) #, colors='b', labeltop=True, labelbottom=False) # x axis
# use matplotlib.colorbar.Colorbar object
cbar = ax.collections[0].colorbar
cbar.ax.tick_params(labelsize=15)
# put the xaxis tick on the top
# ax.xaxis.tick_top()
# ax.tick_params(axis='x',labelsize=6, colors='b', labeltop=True, labelbottom=False)
# rotate the ticklabel, set the fontsize
ax.set_xticklabels(ax.get_xticklabels(), rotation=35, fontsize=15)
ax.set_yticklabels(ax.get_yticklabels(), fontsize=15)
f.savefig('compare_rect.png', dpi=100, bbox_inches='tight')
if __name__ == "__main__":
# print "dataset:"
robot_list = ['purple','blue', 'red', 'orange' ]
sampling_rate = 500
###### run the comparsion for each two robots ######
# run_compare(robot_list, sampling_rate)
# load the dataset
cp_list = np.load("cp_list.npy")
mask = np.load("cp_list_mask.npy")
###### plot heatmap #######
plot_heatmap(robot_list, cp_list)
plt.show()
Figure
References
Colors
cmap
: Python基于seaborn绘制喜欢的热力图,不同色系一览
Another good tutorial for multiple subfigures
Ref:Matplotlib中多子图绘图时,坐标轴及其label的几种排布方式
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,5))
fig.subplots_adjust(wspace = .1,hspace = 0)
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
# ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
col.set_xticks(np.arange(0, 11, 2))
col.set_yticks(np.arange(0, 11, 2))
col.set_xticklabels(np.arange(1, 11, 2))
col.set_yticklabels(np.arange(1, 11, 2))
fig.subplots_adjust(bottom=0, right=0.9, top=1)
cax = plt.axes([0.92, 0.03, 0.03, 0.95])
cb = fig.colorbar(im, cax=cax)
cb.ax.tick_params()
cb.set_label('colorbar')
# plt.tight_layout() # 使用 tight layout 需要手动调整 colorbar 位置,否则会很难看
plt.show()