Python 使用 Seaborn 绘制 Pandas SQL 数据框
Python Plotting Pandas SQL Dataframe with Seaborn
我是数据可视化的新手,正在尝试使用 SQL 输出和 seaborn 制作简单的时间序列图。我很难将从 SQL 查询中检索到的数据插入到 Seaborn 中。关于如何使用 Seaborn 可视化此数据框,您是否可以指导我?
我的Python代码:
#!/usr/local/bin/python3.5
import cx_Oracle
import pandas as pd
from IPython.display import display, HTML
import matplotlib.pyplot as plt
import seaborn as sns
orcl = cx_Oracle.connect('sql_user/sql_pass//sql_database_server.com:9999/SQL_REPORT')
sql = '''
select DATETIME, FRUIT,
COUNTS
from FRUITS.HEALTHY_FRUIT
WHERE DATETIME > '01-OCT-2016'
AND FRUIT = 'APPLE'
'''
curs = orcl.cursor()
df = pd.read_sql(sql, orcl)
display(df)
sns.kdeplot(df)
plt.show()
数据帧 (df) 输出:
DATETIME FRUIT COUNTS
0 2016-10-02 APPLE 1.065757e+06
1 2016-10-03 APPLE 1.064369e+06
2 2016-10-04 APPLE 1.067552e+06
3 2016-10-05 APPLE 1.068010e+06
4 2016-10-06 APPLE 1.067118e+06
5 2016-10-07 APPLE 1.064925e+06
6 2016-10-08 APPLE 1.066576e+06
7 2016-10-09 APPLE 1.065982e+06
8 2016-10-10 APPLE 1.072131e+06
9 2016-10-11 APPLE 1.076429e+06
当我尝试 运行 plt.show() 时,出现以下错误:
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [float64]
而不是 sns.kdeplot
尝试以下操作:
# make time the index (this will help with plot ticks)
df.set_index('DATETIME', inplace=True)
# make figure and axis objects
fig, ax = sns.plt.subplots(1, 1, figsize=(6,4))
df.plot(y='COUNTS', ax=ax, color='red', alpha=.6)
fig.savefig('test.pdf')
plt.show()
如果您要制作折线图,函数 kdeplot()
不是您想要的。它确实画了一条线,但这条线旨在近似变量的分布,而不是显示变量如何随时间变化。到目前为止,制作线图的最简单方法来自 pandas df.plot()
。如果你想要 seaborn 的样式选项,你可以使用 sns.plt.subplots
来创建你的轴对象(我所做的)。您也可以像 中那样使用 sns.set_style()
。
我是数据可视化的新手,正在尝试使用 SQL 输出和 seaborn 制作简单的时间序列图。我很难将从 SQL 查询中检索到的数据插入到 Seaborn 中。关于如何使用 Seaborn 可视化此数据框,您是否可以指导我?
我的Python代码:
#!/usr/local/bin/python3.5
import cx_Oracle
import pandas as pd
from IPython.display import display, HTML
import matplotlib.pyplot as plt
import seaborn as sns
orcl = cx_Oracle.connect('sql_user/sql_pass//sql_database_server.com:9999/SQL_REPORT')
sql = '''
select DATETIME, FRUIT,
COUNTS
from FRUITS.HEALTHY_FRUIT
WHERE DATETIME > '01-OCT-2016'
AND FRUIT = 'APPLE'
'''
curs = orcl.cursor()
df = pd.read_sql(sql, orcl)
display(df)
sns.kdeplot(df)
plt.show()
数据帧 (df) 输出:
DATETIME FRUIT COUNTS
0 2016-10-02 APPLE 1.065757e+06
1 2016-10-03 APPLE 1.064369e+06
2 2016-10-04 APPLE 1.067552e+06
3 2016-10-05 APPLE 1.068010e+06
4 2016-10-06 APPLE 1.067118e+06
5 2016-10-07 APPLE 1.064925e+06
6 2016-10-08 APPLE 1.066576e+06
7 2016-10-09 APPLE 1.065982e+06
8 2016-10-10 APPLE 1.072131e+06
9 2016-10-11 APPLE 1.076429e+06
当我尝试 运行 plt.show() 时,出现以下错误:
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [float64]
而不是 sns.kdeplot
尝试以下操作:
# make time the index (this will help with plot ticks)
df.set_index('DATETIME', inplace=True)
# make figure and axis objects
fig, ax = sns.plt.subplots(1, 1, figsize=(6,4))
df.plot(y='COUNTS', ax=ax, color='red', alpha=.6)
fig.savefig('test.pdf')
plt.show()
如果您要制作折线图,函数 kdeplot()
不是您想要的。它确实画了一条线,但这条线旨在近似变量的分布,而不是显示变量如何随时间变化。到目前为止,制作线图的最简单方法来自 pandas df.plot()
。如果你想要 seaborn 的样式选项,你可以使用 sns.plt.subplots
来创建你的轴对象(我所做的)。您也可以像 sns.set_style()
。