1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonのmatplotlibで目盛り幅自由の数直線を描く

Last updated at Posted at 2019-01-25

はじめに

pythonで目盛り幅が一定でない(対数目盛りなど)数直線を描きたかったが、

  • plt.xscale('log')じゃ色々自由が効かない
  • 等間隔目盛りの方法はあっても目盛り幅を変えられる方法は見つからなかった

ということで色々こねくり回して作ってみた。

環境

  • matplotlib 2.2.2
  • python 3.7.0

数直線表示

実際のコードがこちら。

num_line.py
import matplotlib.pyplot as plt
import numpy as np

def memori(x): # 目盛りにしたい関数
    return  round(np.exp(np.log(255)-(1-x/18)*np.log(255))/255,4)

x = [memori(_) for _ in range(19)] #目盛りの値のリスト

#画像サイズ
fig = plt.figure(figsize=(8,3),dpi=100)

#目盛りに値を表示
plt.text(x[18],0.995 , '1.8', ha = 'center', va = 'top')
plt.text(x[0],0.995 , '0', ha = 'center', va = 'top')
plt.text(x[17],0.995 , '1.7', ha = 'center', va = 'top')
plt.text(x[10],0.995 , '1', ha = 'center', va = 'top')
plt.text(x[15],0.995 , '1.5', ha = 'center', va = 'top')
plt.text(x[16],0.995 , '1.6', ha = 'center', va = 'top')

#元の軸と目盛り消し
plt.xticks(color="None")
plt.yticks(color="None")
plt.tick_params(length=0)

# プロット
plt.plot(x,np.ones(19)*max(x),marker='|',color='k')

↓こんな感じの数直線ができる。
qiita.png

おわりに

今後使う場面あるのか...?と思ったけど備忘録として投稿します。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?