python - 来自 python 字典或 pandas 数据框/系列的日历热图 2 个月

标签 python mysql pandas numpy matplotlib

我有两个月的温度数据,我正在尝试创建日历热图,该热图每天根据当天的温度限制显示为红色或绿色。我为此创建了一本字典,其中包含有关哪一天是绿色或红色的信息。我还创建了 pandas 数据框。

我的数据库表数据如下所示。它仅由两列组成。日期和温度值

usec        temp_data
1464739200  32
1464825600  31.32
1464912000  33.2
1464998400  29.56
.
.
.
.1469923200  28.45

将纪元时间转换为日期时间后,字典看起来像这样

data is here:  {datetime.datetime(2016, 6, 1, 2, 0): 'grey'}
data is here:  {datetime.datetime(2016, 6, 2, 2, 0): 'green'}
.
.
.
data is here:  {datetime.datetime(2016, 7, 29, 2, 0): 'green'}
data is here:  {datetime.datetime(2016, 7, 30, 2, 0): 'green'}
data is here:  {datetime.datetime(2016, 7, 31, 2, 0): 'green'}

我的代码如下:

import datetime
import calendar
import mysql.connector
import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mysql.connector import errorcode


cnx = mysql.connector.connect(user='robbin', password='xxxx', database='rob')
cursor = cnx.cursor()

start_time = 1464739200

query = ("SELECT usec ,`temp_data` "
     "FROM rob_tab WHERE usec >= %s "
     "AND usec <= %s")

for i in range(61):
    current_start_ts = (start_time + (i*86400))
    current_day = datetime.datetime.fromtimestamp(current_start_ts)
    current_end_ts = (start_time + (i*86400)) + 86399
    cursor.execute(query, (current_start_ts , current_end_ts))
    rows = cursor.fetchall()
    rows_arr = np.array(rows)
    print 'type of the rows_arr: ', type(rows_arr)
    data = {}
    if len(rows_arr) == 0:
        data[current_day] = 'grey'
    else:
        for item, index in rows_arr:
            if index >= 34 or index <= 20:
                data[current_day] = 'red'
                break
            else:
                pass
                data[current_day] = 'green'
    daf = pd.DataFrame(data.items(), columns=['Date', 'DateValue']) 

我尝试了下面的一些行,就像上面的代码一样,

    for item, index in data.iteritems():
        print 'index::', index
    # print 'temp values in list: ', item
    events = pd.Series(data[current_day], index=dat)
    print 'events::', events
    calmap.yearplot(events, monthlabels=['june', 'july'])  

and i got the following error:
    index:: grey
    events:: 2016-06-01 02:00:00    grey
    dtype: object
    plot_data = np.ma.masked_where(np.isnan(plot_data), plot_data)
    TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我找到了一个示例,该示例的链接是“Matplotlib and Numpy - Create a calendar heatmap” '。我从 SO 查看这个示例,但无法根据我的代码更改它,因为它与我的情况有点不同。

就我而言,我有字典或 pandas 数据框,而且我只有两个月的时间可以显示。我想使用 pandas dataframe 或 just Dictionary 来解决这个问题,所以我的问题与我提到的示例完全不同,因为在示例中他根本不想使用 pandas 。

如果有人帮助我或指导我创建日历热图,那就太好了。

最佳答案

通过 Google 快速搜索,我找到了 https://pythonhosted.org/calmap/这使得程序员能够用很少的代码制作日历热图。

来自链接:

import numpy as np; np.random.seed(sum(map(ord, 'calmap'))) import pandas as pd import calmap

all_days = pd.date_range('1/15/2014', periods=700, freq='D') days = np.random.choice(all_days, 500) events = pd.Series(np.random.randn(len(days)), index=days)

Using yearplot(), we can easily plot a heatmap of these events over a year:

calmap.yearplot(events, year=2015)

关于python - 来自 python 字典或 pandas 数据框/系列的日历热图 2 个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090340/

相关文章:

python - 分隔某些列中具有相同值的行

php - 从 MySQL 存储和检索 $_GET 语句

python - 将列转换为 Pandas 数据框中的表头

python - 获取已安装包的描述而不实际导入它

python - 将字符串列表中的元素和下一个元素转换为 python 中的日期

python - 本地主机拒绝在调用 google api 的 databricks 笔记本中连接

MySql 复制失败

mysql - 从获取请求中填写选择菜单

python - 如何一次旋转时间序列列的 N 个观察值

python - 如何在二元分类器中按预测概率进行排序?