我对数据可视化很陌生,我试图使用SQL输出和海运绘制一个简单的时间序列图。我很难将从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()Dataframe (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]发布于 2016-10-13 00:49:41
不要使用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()不是你想要的,如果你想要的是一个线状图。它确实是一条线,但该行的目的是近似于变量的分布,而不是显示变量随时间的变化情况。到目前为止,制作线图的最简单的方法是熊猫df.plot()。如果您想要海运的样式选项,可以使用sns.plt.subplots创建您的axis对象(我所做的)。您也可以像在sns.set_style()中一样使用this question。
https://stackoverflow.com/questions/40010317
复制相似问题