用python画6色六边形要图_python – 如何解决六边形图中的边缘问题?

我是初学者,我希望能够清楚地揭露这个问题.

我创建了一个像这样的矩阵:

[0 0 0 0 0 0 0 0]

[0 0 0 0 0 0 0 0]

[0 0 6 8 9 1 0 0]

[0 0 4 6 5 4 0 0]

[0 0 4 2 8 9 0 0]

[0 0 1 3 6 7 0 0]

[0 0 0 0 0 0 0 0]

[0 0 0 0 0 0 0 0]

关键是我必须创建一个六边形图,其中色标表示单个单元格中的随机数.

这就是我做的:

import numpy as np

import matplotlib.pyplot as plt

n=4

A=np.zeros([2*n,2*n], dtype=int)

B=np.random.randint(1,10, size=(n,n))

A[2:6,2:6]=B

plt.figure(figsize=(5,5))

plt.imshow(A, origin=['lower'], cmap=plt.cm.Purples_r)

plt.colorbar()

x=[]

y=[]

for i in range (np.shape(A)[0]):

for j in range (np.shape(A)[1]):

N_occurence=A[i,j]

print(N_occurence)

for k in range (N_occurence):

x=np.append(x, i)

y=np.append(y, j)

plt.figure(figsize=(5,5))

plt.hexbin(x,y,gridsize=(10), cmap=plt.cm.Purples_r)

plt.xlim([1, 6])

plt.ylim([1, 6])

plt.colorbar()

plt.show()

但我无法解决边缘问题,我总是得到半六边形,情节不准确.有谁知道更简单的方法或类似的例子?

解决方法:

我仍然不确定,你在寻找什么,但我想你想要一个使用像hexbin一样的六边形的imshow情节?

也许这有点帮助:

import matplotlib.pyplot as plt

import numpy as np

# Generate array

A = np.zeros([8, 8], dtype=int)

A[2:6, 2:6] = np.random.randint(1, 10, size=(4, 4))

# Print array

print(A)

# `imshow` plot

plt.figure(figsize=(5,5))

plt.imshow(A, extent=(0, 8, 0, 8), origin='lower')

plt.colorbar()

# Rewrite array to get x and y values

# TODO: There has to be a better way than to use two `for` loops

X = []

Y = []

for y in range(len(A)):

for x, n in enumerate(A[len(A)-y-1]):

X += [x]*n

Y += [y]*n

# `scatter` plot to visualize rewritten array data

plt.figure(figsize=(5,5))

plt.scatter(X, Y)

# `hexbin` plot

plt.figure(figsize=(5,5))

plt.hexbin(X, Y, gridsize=5, extent=(0, 7, 0, 7))

plt.colorbar()

# show plots

plt.show()

随机数组A的结果如何

[[0 0 0 0 0 0 0 0]

[0 0 0 0 0 0 0 0]

[0 0 3 7 3 3 0 0]

[0 0 3 5 8 1 0 0]

[0 0 4 8 7 3 0 0]

[0 0 1 7 9 3 0 0]

[0 0 0 0 0 0 0 0]

[0 0 0 0 0 0 0 0]]

imshow

分散

hexbin

我认为使用自定义解决方案可能会更好,例如散点图用您指定的颜色绘制六边形瓷砖.

标签:python,hex,matplotlib,numpy

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值