geometry > numpy > pentagon (五角形)の座標を得る | Matplotlibでの描画 > v0.1, v0.2


動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

処理概要

  • Pentagonの形状を得る
  • Matplotlibで描画する

code v0.1

geometry_starShaped_180414.ipynb
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

'''
v0.1 Apr. 14, 2018
  - add get_pentagon()
      + to obtain pentagon geometry
'''

NUM_VERT = 5  # number of vertices (5: for pentagon)


def get_pentagon(radius):
    xs, ys = [], []
    for idx in range(NUM_VERT):
        theta = 2.0 * np.pi * idx / NUM_VERT
        theta += np.pi / 2.0  # to put the one vertex upward
        xs += [np.cos(theta)]
        ys += [np.sin(theta)]
    return xs, ys

RAD_INNER = 5
RAD_OUTER = 10
xs, ys = get_pentagon(RAD_INNER)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.scatter(xs, ys)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)

code v0.2

  • Pentagonを上向き、下向きにする引数を追加
  • fix bug: get_pentagon() did not use [radius] arg
geometry_starShaped_180414.ipynb
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

'''
v0.2 Apr. 14, 2018
  - fix bug: get_pentagon() did not use [radius] arg
  - get_pentagon() takes [upward] arg
v0.1 Apr. 14, 2018
  - add get_pentagon()
      + to obtain pentagon geometry
'''

NUM_VERT = 5  # number of vertices (5: for pentagon)


def get_pentagon(radius, upward):
    xs, ys = [], []
    for idx in range(NUM_VERT):
        theta = 2.0 * np.pi * idx / NUM_VERT
        if upward:
            theta += np.pi / 2.0  # to put the one vertex upward
        else:
            theta -= np.pi / 2.0  # to put the one vertex downward
        xs += [radius * np.cos(theta)]
        ys += [radius * np.sin(theta)]
    return xs, ys

RAD_INNER = 5
RAD_OUTER = 10
xs, ys = get_pentagon(RAD_INNER, upward=False)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.scatter(xs, ys)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)