Last active
November 23, 2019 13:51
-
-
Save sabopy/945a650bd970815e4f4f875171dedfd2 to your computer and use it in GitHub Desktop.
matplotlibで横軸日付の棒グラフ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
import matplotlib.pyplot as plt | |
import matplotlib.dates as mdates | |
import pandas as pd | |
import numpy as np | |
#load datafile | |
url1 = 'https://sabopy.com/py/barchart_1' | |
data = pd.read_html(url1,header=0)[0] | |
data | |
''' | |
Date Score | |
0 2018-09-23 6418 | |
1 2018-09-24 16065 | |
2 2018-09-25 5201 | |
3 2018-09-26 8995 | |
4 2018-09-27 22515 | |
5 2018-09-28 8436 | |
6 2018-09-29 13638 | |
''' | |
Date = data["Date"].values.astype(np.datetime64) | |
Date | |
''' | |
array(['2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', | |
'2018-09-27', '2018-09-28', '2018-09-29'], dtype='datetime64[D]') | |
''' | |
Score = data['Score'].values | |
Score | |
#array([ 6418, 16065, 5201, 8995, 22515, 8436, 13638]) | |
from pandas.plotting import register_matplotlib_converters | |
register_matplotlib_converters() | |
plt.rcParams["font.size"] = 14 | |
fig = plt.figure(figsize=(6, 4)) | |
ax = fig.add_subplot(111) | |
ax.bar(Date,Score,color = 'pink',edgecolor="black") | |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d')) | |
ax.set_ylabel("Score") # y軸 | |
plt.savefig("Date_Score.png", dpi=120,transparent = False, bbox_inches = 'tight', pad_inches = 0.05) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment