条形图
def plot_tao(x,res):
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Times New Roman'
plt.rcParams['font.weight'] = 'bold'
proposed, b1, b2, b3,b4 = process_res(res)
plt.plot(x, proposed, marker='>', markersize=8, label='Proposed Scheme') # 使用三角形节点
plt.plot(x, b1, marker='o', markersize=8, label='Baseline 1',markerfacecolor='none') # 使用三角形节点
plt.plot(x, b4, marker='*', markersize=8, label='Baseline 2',markerfacecolor='none') # 使用三角形节点
plt.plot(x, b3, marker='d', markersize=8, label='Baseline 3',markerfacecolor='none') # 使用三角形节点
plt.plot(x, b2, marker='s', markersize=8, label='Baseline 4',markerfacecolor='none') # 使用三角形节点
plt.rc('font', size=13)
plt.legend(loc='lower right', ncol=1)
# plt.ylim(2)
plt.grid(linestyle="--",color="gray",linewidth="0.5",axis="both")
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.xlabel(r'Delay Time $T_{\rm delay}$ (ms)', fontsize=13,fontweight='bold')
plt.ylabel('QoE', fontsize=13,fontweight='bold',labelpad=0)
# plt.title('Total QoE at Time Slot')
a = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
plt.savefig("runs/baseline/new_baseline/Tao" + a, dpi=600, bbox_inches='tight', pad_inches=0.01)
# 显示图形
plt.show()
plt.rcParams['font.weight'] = 'bold'
:图里的字(一般为label和坐标轴刻度)设置为粗体;plt.rc('font', size=13)
:设置字体大小markerfacecolor='none'
:maker设置为透明;plt.legend(loc='lower right', ncol=1)
:lower、center、upper;left 、right;plt.xticks(fontsize=13)
:坐标轴刻度的大小plt.ylabel('QoE', fontsize=13,fontweight='bold',labelpad=0)
:设置y轴标题为黑体,labelpad表示标题距离坐标轴刻度的距离,可以设置负数。
柱状图
def plot_Qoe_bar(x,res):
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Times New Roman'
# plt.rcParams['font.weight'] = 'bold'
plt.rc('font', size=13)
plt.grid(linestyle="--", color="gray", linewidth="0.5", axis="both")
proposed, b1, b2, b3 = process_res(res)
categories =[1.8,2.0,2.2,2.4,2.6]
x = np.arange(len(categories))
# 使用plt.bar()替代plt.plot()
width = 0.2 # 设置柱状图的宽度
plt.bar(x, proposed, width=width, label='Proposed Scheme')
plt.bar([i + width for i in x], b1, width=width, label='Baseline 1', alpha=0.7)
plt.bar([i + 2* width for i in x], b3, width=width, label='Baseline 3', alpha=0.7)
plt.bar([i + 3 * width for i in x], b2, width=width, label='Baseline 2', alpha=0.7)
plt.ylim(1000,1700)
plt.xticks([i + 1.5 * width for i in x], categories) # 调整x轴刻度位置
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.xlabel(r'$\gamma$', fontsize=17, labelpad=0)
plt.ylabel('QoE', fontsize=17, labelpad=1)
plt.legend(ncol=2)
a = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
plt.savefig("runs/baseline/Qoe" + a, dpi=600, bbox_inches='tight', pad_inches=0.01)
plt.show()
效果如下:
CDF概率累计分布图
import numpy as np
import matplotlib.pyplot as plt
# 生成一组随机数据
data = np.random.normal(loc=0, scale=1, size=500)
# 对数据进行排序
sorted_data = np.sort(data)
# 计算累积概率
cumulative_prob = np.arange(1, len(sorted_data) + 1) / len(sorted_data)
# 绘制CDF图
plt.plot(sorted_data, cumulative_prob, marker='o', linestyle=None)
plt.xlabel('Data')
plt.ylabel('Cumulative Probability')
plt.title('CDF Plot')
plt.grid(True)
plt.show()
左右双轴画图
def smooth(data, weight=0.9):
'''
Args:
data (List):输入数据
weight (Float): 平滑权重,处于0-1之间,数值越高说明越平滑,一般取0.9
Returns:
smoothed (List): 平滑后的数据
'''
last = data[0] # First value in the plot (first timestep)
smoothed = list()
for point in data:
smoothed_val = last * weight + (1 - weight) * point # 计算平滑值
smoothed.append(smoothed_val)
last = smoothed_val
return smoothed
def pic_reward(r1,r2):
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Times New Roman'
plt.rcParams['font.weight'] = 'bold'
plt.rc('font', size=15)
# 创建子图对象并设置布局为双Y轴
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# 绘制左轴数据
s_r1 = smooth(r1)
s_r2 = smooth(r2)
color = 'tab:orange'
ax1.set_xlabel('Epsiodes',fontsize=17,fontweight='bold',labelpad=-1)
# ax1.set_ylabel('''Reward for Communication and \n Computing Resource Allocation''', color=color,fontsize=17,fontweight='bold',labelpad=0)
ax1.set_ylabel('Reward', color=color,fontsize=17,fontweight='bold',labelpad=0)
ax1.plot(r1, alpha=0.5,color=color)
ax1.plot(s_r1,color=color,label='''Communication and Computing \nResource Allocation''')
ax1.tick_params(axis='y', labelcolor=color)
# ax1.legend(loc='lower right', ncol=1,borderaxespad=2)
# 绘制右轴数据
color = 'tab:green'
# ax2.set_ylabel('Reward for Caching Resource Allocation', color=color,fontsize=17,fontweight='bold',labelpad=1)
ax2.set_ylabel('Reward', color=color,fontsize=17,fontweight='bold',labelpad=2)
ax2.plot(r2, alpha=0.5,color=color)
ax2.plot(s_r2,color=color,label='Caching Resource Allocation')
ax2.tick_params(axis='y', labelcolor=color)
plt.grid(linestyle="--", color="gray", linewidth="0.5", axis="both")
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
# ax2.legend(loc='lower right', ncol=1,borderaxespad=0.5)
plt.rc('font', size=14)
fig.legend(loc='lower right', ncol=1,borderaxespad=4)
# plt.title("Line Chart with Two Axes")
a = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
plt.savefig('runs/baseline/' + a + "_rewards.png", dpi=600, bbox_inches='tight', pad_inches=0.01)
plt.show()
if __name__ == '__main__':
r1=np.load('runs/simulation_res/r1.npy')
r2=np.load('runs/simulation_res/r2.npy')
pic_reward(r1,r2)
效果图: