質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Q&A

解決済

1回答

1581閲覧

Matplotlib 2変量棒グラフのx軸表示について

StartingOver

総合スコア17

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

0グッド

0クリップ

投稿2022/05/01 09:35

前提

お忙しいところ誠に恐縮です。初めてご質問させていただきます。
Matplotlibの基礎の基礎を勉強中です。何卒ご教示いただけますと幸いです。

実現したいこと

以下のコードで、どうしてもxticksにカテゴリー名(labels)を表示させられません。
関数(labels)を引数にして表示させたいです。
頭が悪く、勉強不足で申し訳ございません。可能であれば、理屈をご教授いただけますと有難いです。

発生している問題・エラーメッセージ

ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/axis.py in convert_units(self, x)
1572 try:
-> 1573 ret = self.converter.convert(x, self.units, self)
1574 except Exception as e:

6 frames
ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data

The above exception was the direct cause of the following exception:

ConversionError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/matplotlib/axis.py in convert_units(self, x)
1574 except Exception as e:
1575 raise munits.ConversionError('Failed to convert value(s) to axis '
-> 1576 f'units: {x!r}') from e
1577 return ret
1578

ConversionError: Failed to convert value(s) to axis units: ['K', 'M', 'D', 'Ke', 'A', 'S', 'Y', 'Ma', 'Yo', 'J']

該当のソースコード

言語:Python(Matplotlib)

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 1, 5, 2, 8, 5, 6, 2, 2]
z = [4, 6, 3, 5, 7, 8, 3, 5, 6, 9]

labels = ['K', 'M', 'D', 'Ke', 'A', 'S', 'Y', 'Ma', 'Yo', 'J']

fig, ax = plt.subplots()
ax.bar([n-0.25 for n in x], y,
width=0.25, align='edge', color='darkcyan',label='test1')
ax.bar([n+0.25 for n in x], z,
width=-0.25,align='edge', color='coral', label='test2')
ax.legend(loc='best')

ax.set_xticks(?????????)

試したこと

①ax.set_xticks(labels)
②ax.set_xticks('K', 'M', 'D', 'Ke', 'A', 'S', 'Y', 'Ma', 'Yo', 'J')
③ax.set_xticklabels('K', 'M', 'D', 'Ke', 'A', 'S', 'Y', 'Ma', 'Yo', 'J')

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

python

1import matplotlib.pyplot as plt 2import numpy as np 3 4x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5y = [2, 4, 1, 5, 2, 8, 5, 6, 2, 2] 6z = [4, 6, 3, 5, 7, 8, 3, 5, 6, 9] 7 8labels = ['K', 'M', 'D', 'Ke', 'A', 'S', 'Y', 'Ma', 'Yo', 'J'] 9 10fig, ax = plt.subplots() 11ax.bar([n-0.25 for n in x], y, 12 width=0.25, align='edge', color='darkcyan',label='test1') 13ax.bar([n+0.25 for n in x], z, 14 width=-0.25,align='edge', color='coral', label='test2') 15ax.legend(loc='best') 16 17## 18ax.set_xticks(x) 19ax.set_xticklabels(labels) 20## 21 22plt.show()

イメージ説明

投稿2022/05/01 09:45

melian

総合スコア19798

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問