科学の箱

科学・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

seleniumでWebElement object is not iterableが出るときの対処方法

iterableなオブジェクトを想定して要素を取得、forループに取り込むと下記のようなWebElement object is not iterableが出る。 結論としては勘違い。 Seleniu …

no image

Scrapy – Install

インストールガイドはこちら https://doc.scrapy.org/en/latest/intro/install.html#intro-install anacondaにインストールする co …

no image

タイタニックデータでEDA

タイタニックデータでEDAを実施する。 まずはライブラリの読み込み import pandas as pd import numpy as np import matplotlib.pyplot as …

no image

seabornをEDAに応用する

seabornを利用すれば基本となるビジュアルと分布、regressionをすぐに取得できる。 import seaborn as sns tips = sns.load_dataset(‘tips’ …

no image

Scrapy – Tutorial

Tutorialはこちら https://doc.scrapy.org/en/latest/intro/tutorial.html 特に難しい点はないがいくつかポイント spidersディレクトリはプ …

2019年10月
« 9月   11月 »
 123456
78910111213
14151617181920
21222324252627
28293031  

side bar top



アーカイブ

カテゴリー