//加了下面的代码,博客会禁止复制(代码还可以复制) // document.body.onselectstart = document.body.ondrag = function(){return false;}

Python画图

折线图

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes
import seaborn as sns
import pandas as pd
# x轴数据
x = np.linspace(0, 200, 21)
# y轴数据
y1 = [50, 75, 86, 97, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100];
y2 = [50, 57, 67, 73, 74, 75, 89, 92, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100];
y3 = [50, 56, 71, 74, 76, 79, 79.5, 84, 85, 95, 97.5, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]; 
y4 = [50, 58, 67, 69.5, 76, 81, 92, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100];
y5 = [50, 51, 51.5, 52, 52.5, 54, 59.5, 63, 68, 72, 74, 75, 78, 80, 83, 86, 88, 90, 91.5, 92, 93]; 
# 创建一个新的图形窗口
fig,ax = plt.subplots()
# 绘制折线图
ax.plot(x, y1, color='b', linestyle='-', marker='o',markersize = '5',label='4-bit LSB')
ax.plot(x, y2, color='g', linestyle='-', marker='o', markersize = '5',label='HiDDeN')
ax.plot(x, y3, color='c', linestyle='-', marker='o', markersize = '5',label='Balujs')
ax.plot(x, y4, color='hotpink', linestyle = '-', marker = 'o', markersize = '5',label = 'Weng')
ax.plot(x, y5, color= 'darkorange',linestyle = '-', marker = 'o', markersize = '5',label = 'TPHNiT')
# 添加标题和坐标轴标签
#ax.set_title('多条折线图')
ax.set_xlabel('Number of training samples')
ax.set_ylabel('Accuracy(%)')
xticks = np.linspace(0,200, 11)
ax.set_xticks(xticks)
# 添加图例
ax.legend()
# 显示网格线
ax.grid(True)
# 设置图形边界
#ax.set_xlim(0, 200)
#ax.set_ylim(50, 100)
# 显示图形
plt.savefig('fig1.png')

柱状图

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes
import seaborn as sns
import pandas as pd
x = ['$10^{-1}$', '$10^{0}$', '$10^{1}$', '$10^{2}$', '$10^{3}$']
y1 = [42, 44, 46, 45, 44]
y2 = [48.2, 48.1, 47, 49.5, 50.5]
y3 = [51.5, 51.6, 51.7, 51.5, 51.4]
y4 = [51.6, 51.6, 51.8, 51.6, 51.5] 
y5 = [0.693, 0.672, 0.572, 0.715, 0.728] 
xticks = np.arange(len(x))
fig, ax = plt.subplots();
fig.set_size_inches(8, 6)
ax.set_ylim(40, 52)
# ax = brokenaxes(#xlims=((0, 35), (36, 52)), 设置x轴裂口范围
#                  ylims=((0, 15), (40, 52)),#设置y轴裂口范围
#                  hspace=0.2,#y轴裂口宽度
#                  #wspace=0.2,x轴裂口宽度                 
#                  #despine=False,是否y轴只显示一个裂口
#                  diag_color='r',#裂口斜线颜色 
#                  d = 0.01
#                 )
line1 = ax.bar(xticks, y1, width = 0.2, label = 'PSNR(Cover/Stego)', color = 'darkolivegreen')
line2 = ax.bar(xticks+0.2, y2, width = 0.2, label = 'PSNR(Secret/Recovery)', color = 'yellowgreen')
line3 = ax.bar(xticks+0.4, y3, width = 0.2, label = 'SSIM(Cover/Stego)', color = 'orange')
line4 = ax.bar(xticks+0.6, y4, width = 0.2, label = 'SSIM(Secret/Recovery)', color = 'darkorange')

L1 = ax.legend(handles = [line1, line2], labels = ['PSNR(Cover/Stego)','PSNR(Secret/Recovery)'], bbox_to_anchor=(0, 1.005), loc='lower left')
ax.legend(handles = [line3, line4], labels = ['PSNR(Cover/Stego)','PSNR(Secret/Recovery)'], bbox_to_anchor=(1, 1.005), loc='lower right')
plt.gca().add_artist(L1)
ax.set_xticks(xticks + 0.3)
ax2 = ax.twinx()
ax2.set_ylim(0.5,1.0)
ax2.plot(xticks+0.3, y5, color= 'blue',linestyle = '-', marker = 'o', markersize = '5',label = 'Detection rate')
ax.set_xticks(xticks + 0.3)
ax.set_xticklabels(x)
ax2.legend(bbox_to_anchor=(0.5, 1.07), loc='center')
for i in range(len(xticks)):
    ax2.text(xticks[i]+0.3, y5[i]+0.03, y5[i], fontsize=9, color = "black", style = "italic", weight = "bold", \
         verticalalignment='center', horizontalalignment='center', rotation=0)
ax.set_xlabel('$\lambda_{freq}$')
ax.set_ylabel('PSNR(dB)')
ax2.set_ylabel('SSIM/Detection rete')
plt.savefig('fig2.png')
#ax.set_title("Simple Bar Plot", fontsize=15)

ROC曲线

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes
import seaborn as sns
import pandas as pd
# x轴数据
x = [0.015,0.065, 0.192,0.256, 0.347, 0.433, 0.500, 0.624, 0.762, 0.854, 0.917, 0.995]
y1 = [0.009, 0.092, 0.223,0.283, 0.386, 0.456, 0.500, 0.605,0.723, 0.802, 0.882, 0.987]
y2 = x
# y轴数据
# 创建一个新的图形窗口
fig,ax = plt.subplots()
# 绘制折线图
ax.plot(x, y1, color='DarkOrchid', linestyle='-', marker='o',markersize = '5',label='ROC curve(AUC = 0.5094)')
ax.plot(x, y2, color='g', linestyle='--',label='Random guess')
ax.fill_between(x,0,y2,facecolor = 'LightSkyBlue',alpha = 0.3) 
#ax.set_title('多条折线图')
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
# xticks = np.linspace(0.0,1.0, 11)
# ax.set_xticks(xticks)
# 添加图例
for i in range(len(x)):
    if i < 2:
         ax.text(x[i], y1[i]-0.02, f"({x[i]},{y1[i]})", fontsize=6, color = "black", style = "italic", weight = "bold", \
         verticalalignment='center', horizontalalignment='left', rotation=0)
    else:
        ax.text(x[i], y1[i]+0.02, f"({x[i]},{y1[i]})", fontsize=6, color = "black", style = "italic", weight = "bold", \
        verticalalignment='center', horizontalalignment='right', rotation=0)
ax.legend()
plt.savefig('fig3.png')
# 显示网格线
# 设置图形边界
#ax.set_xlim(0, 200)
#ax.set_ylim(50, 100)
# 显示图形
#plt.savefig('fig1.png')

小提琴图

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes
import seaborn as sns
import pandas as pd
fig, ax = plt.subplots(1,2,constrained_layout=True, figsize=(10, 7))
plt.tight_layout(w_pad=7)
df1 = pd.read_csv("vilion.csv")
df2 = pd.read_csv("vilion2.csv")
axesSub = sns.violinplot(x="category", y="psnr",  scale = 'count',
            data=df1,order = ['Convo.', 'Dense.', 'Resi.'], palette="Set1", ax = ax[0])
#ax[0].yaxis.set_major_locator(plt.NullLocator())同时隐藏刻度和坐标轴标签
#ax[0].xaxis.set_major_formatter(plt.NullFormatter())隐藏坐标轴标签
ax[0].set_xlabel('')
sns.violinplot(x="category", y="psnr",  scale = 'count',
            data=df2,order = ['Convo.', 'Dense.', 'Resi.'], palette="Set2", ax = ax[1])
ax[1].set_xlabel('')
yticks = np.linspace(35, 60, 6)
ax[0].set_yticks(yticks)
ax[1].set_yticks(yticks)
# font = {'family' : 'Times New Roman',
# 'weight' : 'normal',
# 'size'   : 20,
#         }
plt.subplots_adjust(left = 0.1, bottom = 0.1)
ax[0].set_ylabel('PSNR(db)    Cover/Stego',fontsize = 15)
ax[1].set_ylabel('PSNR(db)    Secret/Recovery',fontsize =  15)
plt.title(r'Different architecture of $\rho(\cdot),\varphi(\cdot),\eta (\cdot)$', x = -0.2, y = -0.1, fontsize = 14)
plt.savefig('fig4.png')

3D图

import numpy as np
import matplotlib.pyplot as plt

# 定义山的表面方程
def mountain_surface(x, y):
    return 1 - x**2 - 2*y**2

# 定义梯度计算函数
def gradient(x, y):
    dx = -(2 * x)
    dy = -(4 * y)
    return dx, dy

# 初始位置
x = 0.5
y = 0.5
z = 0.25
learning_rate = 0.1  # 学习率
iterations = 20    # 迭代次数

# 存储线路坐标
path = [(x, y, z)]

# 执行梯度上升
for _ in range(iterations):
    dx, dy = gradient(x, y)
    x += learning_rate * dx
    y += learning_rate * dy
    z = mountain_surface(x, y)
    path.append((x, y, z))

# 提取x, y, z坐标
path_x = [point[0] for point in path]
path_y = [point[1] for point in path]
path_z = [point[2] for point in path]

# 绘制山体形状
x_range = np.linspace(-0.75, 0.75, 400)
y_range = np.linspace(-0.75, 0.75, 400)
X, Y = np.meshgrid(x_range, y_range)
Z = mountain_surface(X, Y)

# 创建二维图形
plt.figure(figsize=(10, 8))
plt.contourf(X, Y, Z, cmap='viridis', levels=50)
plt.colorbar()
plt.scatter(path_x[0], path_y[0], c='g', marker='o', label='P')
plt.scatter(path_x[-1], path_y[-1], c='r', marker='o', label='Peak')
plt.plot(path_x, path_y, 'bx-')
plt.title("Mountain Climb in 2D")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.show()

# 创建三维图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制山体形状
x_range = np.linspace(-0.75, 0.75, 100)
y_range = np.linspace(-0.75, 0.75, 100)
X, Y = np.meshgrid(x_range, y_range)
Z = mountain_surface(X, Y)
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)

# 绘制线路图
ax.scatter(path_x[0], path_y[0], path_z[0], c='g', marker='o', label='P')
ax.scatter(path_x[-1], path_y[-1], path_z[-1], c='r', marker='o', label='Peak')
ax.plot(path_x, path_y, path_z, 'bx-')
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.set_title("Mountain Climb in 3D")
ax.legend()
plt.show() 
posted @ 2024-03-29 11:24  龙鳞墨客  阅读(74)  评论(0)    收藏  举报