如何让matplotlib.hexbin绘制的六边形实现平边向上的显示效果
hexbin平边朝上显示实现方法
可以实现,matplotlib.pyplot.hexbin内置了朝向控制参数,无需额外做坐标变换:
- 默认
orientation='vertical':对应尖边沿y轴朝上的默认效果 - 设置
orientation='horizontal':即可实现平边朝上的90度旋转效果
修改后的完整代码如下:
import numpy as np import matplotlib.pyplot as plt x = np.random.rand(1000)*2-1 y = np.random.rand(1000)*2-1 plt.hexbin(x, y, gridsize=10, orientation='horizontal') plt.show()
如果遇到低版本matplotlib不支持orientation参数的场景,可以用交换x、y输入值的替代方案,同步调整轴标签即可:
import numpy as np import matplotlib.pyplot as plt x = np.random.rand(1000)*2-1 y = np.random.rand(1000)*2-1 # 交换x、y输入实现旋转效果 plt.hexbin(y, x, gridsize=10) # 按需修正轴标签 plt.xlabel('原x轴对应数值') plt.ylabel('原y轴对应数值') plt.show()
内容的提问来源于stack exchange,提问作者Thomas Wagenaar
相关产品推荐
相关产品推荐

