画一个距离权重的图

import numpy as np
import matplotlib.pyplot as plt

# 创建一个19x19的数组,并将所有值初始化为0
image_size = 17
image = np.zeros((image_size, image_size))

# 设置中心点的值为10
center_x, center_y = image_size // 2, image_size // 2
image[center_x, center_y] = 9

# 逐渐减小每个格点的值
for r in range(1, image_size // 2 + 1):
    for i in range(center_x - r, center_x + r + 1):
        for j in range(center_y - r, center_y + r + 1):
            if 0 <= i < image_size and 0 <= j < image_size:
                distance = np.sqrt((i - center_x) ** 2 + (j - center_y) ** 2)
                image[i, j] = 10 - distance

# 绘制图像
plt.imshow(image, cmap='rainbow', interpolation='nearest')
# 添加网格线
for i in range(image_size + 1):
    plt.axhline(y=i-0.5, color='lightgrey', lw=0.2)
    plt.axvline(x=i-0.5, color='lightgrey', lw=0.2)
# 禁用x轴和y轴上的刻度值
plt.xticks([])
plt.yticks([])
# plt.colorbar()

# 添加图例并设置字体为新罗马体
# legend = plt.legend(['distance'], loc='upper right', prop={'family': 'serif'})

# 添加颜色条标签为"distance"并设置字体为新罗马体
cbar = plt.colorbar()
cbar.set_label('distance', family='serif')

# 自定义颜色条的刻度和标签
cbar.set_ticks([0,2,4,6,8,10])
cbar.set_ticklabels(['0','2','4', '6','8',' ' ])
# 使用annotate方法将标签放在颜色条上方,设置为两行
cbar.ax.annotate('center point', xy=(0.8, 9.5), xytext=(0, 20), textcoords="offset points", ha='center', va='center')


plt.show()

结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值