科学の箱

科学・IT・登山の話題

Python

pyplotでx軸のラベルを90度回転させる

投稿日:

pyplotでx軸にラベルを記載するとラベル文字数が長すぎるためにお互いにオーバーラップしてみにくい。

このよう場合にはpyplot.xticksを実行する際にrotationを指定すればよい。

Rotating custom tick labels — Matplotlib 3.1.1 documentation

rotationはmatplotlib.textで定義されている属性の一部である。ほかにも様々な属性を指定できる。

matplotlib.text

まずrotationを指定しない場合を見てみる。

import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels)
plt.show()

次にrotationを指定する。

import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
plt.show()

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

pythonでTensorFlowを使うまで

TensorFlowが利用できるまでの設定手順をまとめる。 まず基本となるインストラクションはこちらにある。 https://www.tensorflow.org/install/pip このインスト …

no image

pyperclipを設定する

pyperclipはpip3経由でインストールする。まずはpip3のインストールから。 https://bootstrap.pypa.io/get-pip.pyからget-pip.pyをダウンロードし …

no image

OpenCV

WindowsにOpenCVをインストールする場合に2つのやり方がある。 一つは様々な言語からOpenCVを利用できるようにする方法、2つ目の方法ではPythonからOpenCVを利用する方法である。 …

no image

flip, fliplr, flipudを使って配列要素を上下左右、左右、上下反転する

画像処理などでは要素の値を配列全体で反転したいときがある。このようなときに役に立つのがflip, fliplr, flipudである。 flipのマニュアル fliprのマニュアル flipudのマニ …

no image

kaggle Titanic Tutorial – 5

ファミリーサイズを追加してみる。 ソースはこちらを参照した。 https://www.kaggle.com/lperez/titanic-a-deeper-look-on-family-size 主要 …

2019年10月
月 火 水 木 金 土 日
« 9月   11月 »
 123456
78910111213
14151617181920
21222324252627
28293031  

side bar top



アーカイブ

カテゴリー