Home > other >  Plotting hhmm (int) format time data on matplotlib polar graph
Plotting hhmm (int) format time data on matplotlib polar graph

Time:12-22

I am plotting data which is currently formatted as integers (hhmm) as times of day onto a polar matplotlib graph and have come up with the dilemma that because of this integer format, I have gaps caused by the last 40 integers in every hundred never being plotted as in my data something like 1372 as 1:72, for example, doesn't make sense and doesn't exist.

I have three 'paths' the way I see it to fix this and those are; convert the hhmm data to proper time data to allow for easier plotting, temporarily changing the string value of the integer to 'stretch' over the hour or changing the x-axis of the graph to somehow omit the last 40 integers in every hundred.

I have tried the second by attempting to alter the last two characters in each of the hhmm values as strings and multiplying them by (5/3) to stretch them over the hour. This has come up with numerous problems like extra characters where they are not needed and bad handling of 0 when converted from string to int.

I figure that converting the data to a proper format is the best idea but I'm not even sure how I would go about performing this as I can only see an explanation of how to do this in SQL which I have no experience with.

    # The code how it appears in my actual program is below

    # Clears any previous data
    plt.clf()

    # Data that goes in via 'dataList'
    dataList = [640, 641, 650, 703, 712, 740, 742, 756, 814, 816, 817, 820, 823, 824, 833, 839, 840, 850, 907, 910, 912, 914, 918, 918, 919, 922, 923, 925, 928, 929, 930, 932, 932, 935, 935, 936, 936, 936, 936, 938, 939, 941, 941, 941, 941, 942, 943, 943, 944, 945, 945, 946, 946, 948, 948, 948, 949, 949, 952, 952, 953, 953, 954, 956, 957, 958, 958, 1000, 1002, 1004, 1004, 1004, 1004, 1008, 1009, 1010, 1011, 1012, 1012, 1018, 1021, 1022, 1023, 1024, 1030, 1030, 1031, 1034, 1034, 1037, 1040, 1041, 1041, 1047, 1049, 1050, 1051, 1053, 1056, 1102, 1105, 1105, 1105, 1105, 1105, 1107, 1107, 1108, 1109, 1110, 1110, 1111, 1113, 1115, 1115, 1116, 1117, 1117, 1118, 1118, 1120, 1121, 1125, 1127, 1128, 1128, 1130, 1134, 1134, 1134, 1136, 1136, 1137, 1137, 1137, 1143, 1145, 1147, 1148, 1149, 1153, 1156, 1158, 1200, 1202, 1206, 1208, 1208, 1208, 1211, 1211, 1212, 1213, 1214, 1220, 1222, 1222, 1223, 1223, 1224, 1225, 1229, 1229, 1229, 1231, 1231, 1233, 1233, 1233, 1235, 1237, 1239, 1240, 1241, 1241, 1242, 1243, 1245, 1247, 1248, 1249, 1250, 1251, 1251, 1252, 1256, 1256, 1258, 1259, 1301, 1301, 1302, 1303, 1303, 1305, 1306, 1308, 1308, 1308, 1312, 1314, 1315, 1318, 1318, 1320, 1320, 1321, 1322, 1323, 1323, 1325, 1327, 1328, 1328, 1328, 1330, 1330, 1331, 1332, 1335, 1335, 1337, 1338, 1338, 1339, 1341, 1342, 1344, 1345, 1346, 1346, 1347, 1348, 1350, 1350, 1351, 1352, 1354, 1356, 1356, 1356, 1358, 1358, 1359, 1401, 1404, 1406, 1407, 1409, 1415, 1415, 1416, 1420, 1423, 1424, 1425, 1425, 1426, 1427, 1428, 1428, 1430, 1433, 1434, 1437, 1439, 1440, 1441, 1444, 1445, 1445, 1445, 1445, 1445, 1447, 1448, 1448, 1449, 1449, 1449, 1450, 1451, 1451, 1451, 1451, 1452, 1454, 1456, 1456, 1458, 1459, 1500, 1503, 1505, 1505, 1508, 1509, 1511, 1512, 1515, 1516, 1519, 1519, 1521, 1521, 1523, 1526, 1527, 1527, 1529, 1529, 1530, 1531, 1532, 1533, 1534, 1534, 1536, 1537, 1539, 1540, 1540, 1540, 1541, 1541, 1541, 1545, 1546, 1547, 1553, 1554, 1556, 1556, 1557, 1559, 1600, 1600, 1601, 1601, 1601, 1602, 1604, 1606, 1611, 1611, 1611, 1614, 1617, 1620, 1622, 1625, 1626, 1626, 1626, 1626, 1627, 1629, 1629, 1632, 1632, 1633, 1633, 1633, 1635, 1636, 1639, 1639, 1640, 1640, 1641, 1642, 1644, 1645, 1645, 1646, 1648, 1652, 1652, 1653, 1653, 1654, 1657, 1657, 1702, 1706, 1711, 1712, 1714, 1715, 1716, 1717, 1717, 1726, 1729, 1732, 1733, 1735, 1736, 1737, 1741, 1742, 1745, 1747, 1751, 1758, 1801, 1801, 1802, 1803, 1803, 1819, 1822, 1824, 1829, 1829, 1832, 1833, 1836, 1839, 1840, 1842, 1844, 1845, 1850, 1859, 1859, 1905, 1911, 1930, 1940, 2026, 2038, 2159, 2230, 2233]

    numColumns = ["temperature", "humidity", "#"]
    f = Figure(figsize=(6, 6), dpi=100)
    # Embedding plot onto Tkinter GUI
    canvas = FigureCanvasTkAgg(f, master=frame)
    canvas.get_tk_widget().grid(row=0, column=7, sticky='nes',
                                padx=(15, 15))

    if column == 'time':

        p = f.add_subplot(projection='polar')
        angles = (np.array(dataList) / (20 / 3)) * pi / 180
        p.bar(angles, np.full(len(dataList), 0.9), width=0.01,
              bottom=0.89, linewidth=0)

        plt.setp(p.get_yticklabels(), visible=False)

        p.set_xticks(np.linspace(0, 2 * pi, 24, endpoint=False))
        p.set_xticklabels(range(24))

        p.set_theta_direction(-1)

        p.set_theta_offset(pi / 2.0)

        plt.ylim(0, 1)

Current output with the obvious gaps at xx61

       # Code & data to reproduce the plot

       from matplotlib import pyplot as plt
       from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
       from matplotlib.figure import Figure
       from numpy import pi
    
       root = Tk()
       dataList = [640, 641, 650, 703, 712, 740, 742, 756, 814, 816, 817, 820, 823, 824, 833, 839, 840, 850, 907, 910, 912, 914, 918, 918, 919, 922, 923, 925, 928, 929, 930, 932, 932, 935, 935, 936, 936, 936, 936, 938, 939, 941, 941, 941, 941, 942, 943, 943, 944, 945, 945, 946, 946, 948, 948, 948, 949, 949, 952, 952, 953, 953, 954, 956, 957, 958, 958, 1000, 1002, 1004, 1004, 1004, 1004, 1008, 1009, 1010, 1011, 1012, 1012, 1018, 1021, 1022, 1023, 1024, 1030, 1030, 1031, 1034, 1034, 1037, 1040, 1041, 1041, 1047, 1049, 1050, 1051, 1053, 1056, 1102, 1105, 1105, 1105, 1105, 1105, 1107, 1107, 1108, 1109, 1110, 1110, 1111, 1113, 1115, 1115, 1116, 1117, 1117, 1118, 1118, 1120, 1121, 1125, 1127, 1128, 1128, 1130, 1134, 1134, 1134, 1136, 1136, 1137, 1137, 1137, 1143, 1145, 1147, 1148, 1149, 1153, 1156, 1158, 1200, 1202, 1206, 1208, 1208, 1208, 1211, 1211, 1212, 1213, 1214, 1220, 1222, 1222, 1223, 1223, 1224, 1225, 1229, 1229, 1229, 1231, 1231, 1233, 1233, 1233, 1235, 1237, 1239, 1240, 1241, 1241, 1242, 1243, 1245, 1247, 1248, 1249, 1250, 1251, 1251, 1252, 1256, 1256, 1258, 1259, 1301, 1301, 1302, 1303, 1303, 1305, 1306, 1308, 1308, 1308, 1312, 1314, 1315, 1318, 1318, 1320, 1320, 1321, 1322, 1323, 1323, 1325, 1327, 1328, 1328, 1328, 1330, 1330, 1331, 1332, 1335, 1335, 1337, 1338, 1338, 1339, 1341, 1342, 1344, 1345, 1346, 1346, 1347, 1348, 1350, 1350, 1351, 1352, 1354, 1356, 1356, 1356, 1358, 1358, 1359, 1401, 1404, 1406, 1407, 1409, 1415, 1415, 1416, 1420, 1423, 1424, 1425, 1425, 1426, 1427, 1428, 1428, 1430, 1433, 1434, 1437, 1439, 1440, 1441, 1444, 1445, 1445, 1445, 1445, 1445, 1447, 1448, 1448, 1449, 1449, 1449, 1450, 1451, 1451, 1451, 1451, 1452, 1454, 1456, 1456, 1458, 1459, 1500, 1503, 1505, 1505, 1508, 1509, 1511, 1512, 1515, 1516, 1519, 1519, 1521, 1521, 1523, 1526, 1527, 1527, 1529, 1529, 1530, 1531, 1532, 1533, 1534, 1534, 1536, 1537, 1539, 1540, 1540, 1540, 1541, 1541, 1541, 1545, 1546, 1547, 1553, 1554, 1556, 1556, 1557, 1559, 1600, 1600, 1601, 1601, 1601, 1602, 1604, 1606, 1611, 1611, 1611, 1614, 1617, 1620, 1622, 1625, 1626, 1626, 1626, 1626, 1627, 1629, 1629, 1632, 1632, 1633, 1633, 1633, 1635, 1636, 1639, 1639, 1640, 1640, 1641, 1642, 1644, 1645, 1645, 1646, 1648, 1652, 1652, 1653, 1653, 1654, 1657, 1657, 1702, 1706, 1711, 1712, 1714, 1715, 1716, 1717, 1717, 1726, 1729, 1732, 1733, 1735, 1736, 1737, 1741, 1742, 1745, 1747, 1751, 1758, 1801, 1801, 1802, 1803, 1803, 1819, 1822, 1824, 1829, 1829, 1832, 1833, 1836, 1839, 1840, 1842, 1844, 1845, 1850, 1859, 1859, 1905, 1911, 1930, 1940, 2026, 2038, 2159, 2230, 2233]
    
       f = Figure(figsize=(6, 6), dpi=100)
       # Embedding plot onto Tkinter GUI
       canvas = FigureCanvasTkAgg(f, master=root)
       canvas.get_tk_widget().grid(row=0, column=7, sticky='nes',
                                padx=(15, 15))
       p = f.add_subplot(projection='polar')
       angles = (np.array(dataList) / (20 / 3)) * pi / 180
       p.bar(angles, np.full(len(dataList), 0.9), width=0.01,
          bottom=0.89, linewidth=0)
       plt.setp(p.get_yticklabels(), visible=False)
       p.set_xticks(np.linspace(0, 2 * pi, 24, endpoint=False))
       p.set_xticklabels(range(24))
       p.set_theta_direction(-1)
       p.set_theta_offset(pi / 2.0)
       plt.ylim(0, 1)
    
       root.mainloop()

CodePudding user response:

Interpolating (spreading over an interval 0-100 i/o 0-60) the minutes in your dataset (keeping the hours as they are) produces the following plot, which I think alleviates the "gap" problem you mentioned.

enter image description here

    from tkinter import *
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    from numpy import pi
    
    
    
    def interpolate_clock_to_dec(val):
        hours = (val // 100) * 100
        minutes = val - hours
        decimin = minutes / 60 * 100
        return hours   decimin
        
    def convert_continuous(data):
        converted = []
        for val in data:
            converted.append(interpolate_clock_to_dec(val))
        return converted
    
    root = Tk()
    dataList = [640, 641, 650, 703, 712, 740, 742, 756, 814, 816, 817, 820, 823, 824, 833, 839, 840, 850, 907, 910, 912, 914, 918, 918, 919, 922, 923, 925, 928, 929, 930, 932, 932, 935, 935, 936, 936, 936, 936, 938, 939, 941, 941, 941, 941, 942, 943, 943, 944, 945, 945, 946, 946, 948, 948, 948, 949, 949, 952, 952, 953, 953, 954, 956, 957, 958, 958, 1000, 1002, 1004, 1004, 1004, 1004, 1008, 1009, 1010, 1011, 1012, 1012, 1018, 1021, 1022, 1023, 1024, 1030, 1030, 1031, 1034, 1034, 1037, 1040, 1041, 1041, 1047, 1049, 1050, 1051, 1053, 1056, 1102, 1105, 1105, 1105, 1105, 1105, 1107, 1107, 1108, 1109, 1110, 1110, 1111, 1113, 1115, 1115, 1116, 1117, 1117, 1118, 1118, 1120, 1121, 1125, 1127, 1128, 1128, 1130, 1134, 1134, 1134, 1136, 1136, 1137, 1137, 1137, 1143, 1145, 1147, 1148, 1149, 1153, 1156, 1158, 1200, 1202, 1206, 1208, 1208, 1208, 1211, 1211, 1212, 1213, 1214, 1220, 1222, 1222, 1223, 1223, 1224, 1225, 1229, 1229, 1229, 1231, 1231, 1233, 1233, 1233, 1235, 1237, 1239, 1240, 1241, 1241, 1242, 1243, 1245, 1247, 1248, 1249, 1250, 1251, 1251, 1252, 1256, 1256, 1258, 1259, 1301, 1301, 1302, 1303, 1303, 1305, 1306, 1308, 1308, 1308, 1312, 1314, 1315, 1318, 1318, 1320, 1320, 1321, 1322, 1323, 1323, 1325, 1327, 1328, 1328, 1328, 1330, 1330, 1331, 1332, 1335, 1335, 1337, 1338, 1338, 1339, 1341, 1342, 1344, 1345, 1346, 1346, 1347, 1348, 1350, 1350, 1351, 1352, 1354, 1356, 1356, 1356, 1358, 1358, 1359, 1401, 1404, 1406, 1407, 1409, 1415, 1415, 1416, 1420, 1423, 1424, 1425, 1425, 1426, 1427, 1428, 1428, 1430, 1433, 1434, 1437, 1439, 1440, 1441, 1444, 1445, 1445, 1445, 1445, 1445, 1447, 1448, 1448, 1449, 1449, 1449, 1450, 1451, 1451, 1451, 1451, 1452, 1454, 1456, 1456, 1458, 1459, 1500, 1503, 1505, 1505, 1508, 1509, 1511, 1512, 1515, 1516, 1519, 1519, 1521, 1521, 1523, 1526, 1527, 1527, 1529, 1529, 1530, 1531, 1532, 1533, 1534, 1534, 1536, 1537, 1539, 1540, 1540, 1540, 1541, 1541, 1541, 1545, 1546, 1547, 1553, 1554, 1556, 1556, 1557, 1559, 1600, 1600, 1601, 1601, 1601, 1602, 1604, 1606, 1611, 1611, 1611, 1614, 1617, 1620, 1622, 1625, 1626, 1626, 1626, 1626, 1627, 1629, 1629, 1632, 1632, 1633, 1633, 1633, 1635, 1636, 1639, 1639, 1640, 1640, 1641, 1642, 1644, 1645, 1645, 1646, 1648, 1652, 1652, 1653, 1653, 1654, 1657, 1657, 1702, 1706, 1711, 1712, 1714, 1715, 1716, 1717, 1717, 1726, 1729, 1732, 1733, 1735, 1736, 1737, 1741, 1742, 1745, 1747, 1751, 1758, 1801, 1801, 1802, 1803, 1803, 1819, 1822, 1824, 1829, 1829, 1832, 1833, 1836, 1839, 1840, 1842, 1844, 1845, 1850, 1859, 1859, 1905, 1911, 1930, 1940, 2026, 2038, 2159, 2230, 2233]
    
    converted_data = convert_continuous(dataList)
    
    f = Figure(figsize=(6, 6), dpi=100)
    # Embedding plot onto Tkinter GUI
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.get_tk_widget().grid(row=0, column=7, sticky='nes', padx=(15, 15))
    p = f.add_subplot(projection='polar')
    angles = (np.array(converted_data) / (20 / 3)) * pi / 180
    p.bar(angles, np.full(len(converted_data), 0.9), width=0.01, bottom=0.89, linewidth=0)
    plt.setp(p.get_yticklabels(), visible=False)
    p.set_xticks(np.linspace(0, 2 * pi, 24, endpoint=False))
    p.set_xticklabels(range(24))
    p.set_theta_direction(-1)
    p.set_theta_offset(pi / 2.0)
    plt.ylim(0, 1)
    plt.show()
    
    root.mainloop()
  • Related