我使用此代码制作在此处显示在同一图表上的图表。我想将它们放在单独的图表/子图上。我怎样才能做到这一点?有很多示例,但大多数示例都包含csv文件或随机数。在这种情况下,我无法使用mysql找到一个好的绘图教程。谢谢。
import mysql.connector
import matplotlib.pyplot as plt
import pandas
# connect to MySQL database
conn = mysql.connector.MySQLConnection(user='root', password='',
host='127.0.0.1', database='DB')
query = """SELECT Time, Stag, Mean, Diff, Var, Ment, Med FROM Maintab;"""
df = pandas.read_sql(query, conn, index_col=['Time'])
fig, ax = plt.subplots()
df=df.astype(float)
df.plot(ax=ax)
conn.close()
经过一些试验,到目前为止,我已经做到了:
import mysql.connector
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
# connect to MySQL database
conn = mysql.connector.MySQLConnection(user='root', password='',
host='127.0.0.1', database='DB')
cursor = conn.cursor()
cursor.execute('SELECT Time, Stag, Mean, Diff FROM Maintab');
rows = cursor.fetchall()
#print(rows)
df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: 'Time', 1: 'Stag', 2: 'Mean', 3: 'Diff'}, inplace=True);
df = df.astype('int')
df["Time"] = df['Time'].astype('int')
df["Stag"] = df['Stag'].astype('int')
df["Mean"] = df['Mean'].astype('int')
df["Diff"] = df['Diff'].astype('int')
plt.figure()
plt.subplot(2,1,1)
plt.title('Stag')
plt.plot(df["Time"],'b-',label=r'$T_1$')
plt.plot(df['Stag'],'g:',label=r'$T_2$')
plt.legend(loc='best')
plt.figure()
plt.subplot(2,1,2)
plt.title('Mean')
plt.plot(df["Time"],'b-',label=r'$T_1$')
plt.plot(df['Mean'],'g:',label=r'$T_2$')
plt.legend(loc='best')
这给了我这个结果: