【问题标题】:calendar Heatmap from python dictionary or pandas dataframe/series for 2 months来自 python 字典或 pandas 数据框/系列的日历热图 2 个月
【发布时间】:2017-10-20 18:45:42
【问题描述】:

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

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

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。

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

【问题讨论】:

  • 您有图表/图像应该是什么样子的示例吗?
  • @ScottBoston 是的,我将编辑我的问题并添加图像应该是什么样子。非常感谢您的帮助
  • 没有明确的问题描述。此外,如果您找到了一个示例,请链接到它,这样人们实际上可以帮助您调整它以适应您的需求。最后,提供minimal reproducible example 会大大增加您获得帮助的机会。
  • @ImportanceOfBeingErnest 感谢您的指导。我现在将添加一个链接。
  • 这是一个很棒的代码。你需要的一切都已经存在了。除非您确切地告诉您在使用此代码时遇到了什么问题,否则这里无能为力。

标签: python mysql pandas numpy matplotlib


【解决方案1】:

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

来自链接:

将 numpy 导入为 np; np.random.seed(sum(map(ord, 'calmap'))) 将熊猫导入为 pd 导入平静地图

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)

使用 yearplot(),我们可以轻松绘制这些事件在一年内的热图:

calmap.yearplot(events, year=2015)

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 2018-05-04
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2016-02-10
    • 2014-09-28
    相关资源
    最近更新 更多