前提
お忙しいところ誠に恐縮です。初めてご質問させていただきます。
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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。