test1.py
import sqlalchemy
import pandas as pd
import matplotlib.pyplot as plt
#SQLAlchemy initialization
CONNECT_INFO = 'mssql+pyodbc://hogehoge'
engine = sqlalchemy.create_engine(CONNECT_INFO, encoding='utf-8')
#Variable setting
qq = 1
ym = 201607
#DB connection, stored execution, pandas data frame creation
query = 'EXEC dbo.sp_rtrv_hogehoge @q = {0},@prd = {1}'.format(qq,ym )
df = pd.read_sql_query(query, engine ,index_col =['t'])
#Graph drawing
ax = df.plot( color=('b','r') , alpha=0.6 )
plt.title('hogehoge_title')
plt.grid(which='major')
#ax = plt.gca()
#Save file
fname ='test'+ str(ym) +'.png'
plt.savefig(fname)
plt.close()
-Plot () of pandas (df) is a wrapper method of plt.plot () of matplotlib. -In the above, the SQLSevrer stored procedure is executed (in SQL Server, the stored procedure is executed by EXEC). -Stored arguments (qq and ym) are specified in python variable embedded notation. -You can get the graph currently being edited with ʻax = plt.gca () `
-In the above, no Y-axis scale is applied, so for example, in the case of 1 million, it is written as "1000000" as it is.
Y-axis scale ↓
test1.py
import sqlalchemy
import pandas as pd
import matplotlib.pyplot as plt
#(Omission)
#Graph drawing
ax = df.plot( color=('b','r') , alpha=0.6 )
#ax = plt.gca()
ax.ticklabel_format(style="sci", axis="y",scilimits=(0,0))
Y-axis scale ↓
・ It became a scale like "Data Scientist" (laughs) However, with the notation of le6, there is a risk that the boss of the liberal arts Osan, who is weak in mecha, will become a boon (white eyes), so the next ex3 was fine-tuned.
test1.py
import sqlalchemy
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
#(Omission)
#Graph drawing
ax = df.plot( color=('b','r') , alpha=0.6 )
#ax = plt.gca()
ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
ax.ticklabel_format(style="sci", axis="y",scilimits=(0,0))
-If you set the property of ScalarFormatter to ʻuseMathText = True`, it becomes a power notation of 10. ・ One million is $ 1 × 10 ^ 6 $ (** 10 to the 6th power **, that is, 6 zeros)
Y-axis scale ↓
test4.py
import sqlalchemy
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
#Class settings * Inherit ScalarFormatter
class FixedOrderFormatter(ScalarFormatter):
def __init__(self, order_of_mag=0, useOffset=True, useMathText=True):
self._order_of_mag = order_of_mag
ScalarFormatter.__init__(self, useOffset=useOffset,
useMathText=useMathText)
def _set_orderOfMagnitude(self, range):
self.orderOfMagnitude = self._order_of_mag
#(Omission)
#Graph drawing
ax = df.plot( color=('b','r') , alpha=0.6 )
#ax = plt.gca()
ax.yaxis.set_major_formatter(FixedOrderFormatter(4 ,useMathText=True))
#ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
ax.ticklabel_format(style="sci", axis="y",scilimits=(0,0))
・ Depending on the data, you may want to use 10,000 units ($ 1 x 10 ^ 4
Y-axis scale ↓
・ 10,000 is $ 1 × 10 ^ 4 $ (** 10 to the 4th power **, that is, 4 zeros), so 1 million is $ 100 × 10 ^ 4 $
・ [Matplotlib] I want to use the scale of the axis as exponential notation (via. · Matplotlib: format axis offset-values to whole numbers or specific numbers (Via.stackOverflow) ・ Github: matplotlib / lib / matplotlib / ticker.py
Recommended Posts