LoginSignup
2
3

More than 5 years have passed since last update.

Matplotlibの棒グラフのX軸の表記について

Last updated at Posted at 2017-08-23

棒グラフの場合

配列bに日付文字列を「適度な間隔」で格納してset_xticklabelsでセット

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

ax = df.plot(kind='bar', color=['b','r','g'] , label ='hoge' ,alpha=0.3 )  
#ax = plt.gca()

ln = len(df.index)
a = np.arange(ln)+1
b = []
for i in a-1:
    b.append("") if i%5!=0 else b.append(str(df.index[i].strftime('%m/%d')))
#print(b)
ax.set_xticklabels(b,rotation =15)

折れ線グラフの場合

X軸の表記はax.xaxis.set_major_formatterなどでセット

hoge.py
ax = df.plot(kind='line', color=['b','r','g'] , label ='hoge' ,alpha=0.3 )  

years = mdates.MonthLocator(interval=3)
months = mdates.MonthLocator()
daysFmt = mdates.DateFormatter("%m/%d")

ax.xaxis.set_major_locator(years)
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_major_formatter( daysFmt )

2
3
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
2
3