我正在將當前格式化為整數 (hhmm) 的資料繪制到極坐標 matplotlib 圖形上,并提出了一個困境,即由于這種整數格式,我有每百個中的最后 40 個整數造成的差距從未被例如,在我的資料中繪制的 1372 為 1:72 是沒有意義的,也不存在。
我認為解決這個問題的方式有三個“路徑”,它們是;將 hhmm 資料轉換為適當的時間資料以允許更輕松地繪圖,臨時將整數的字串值更改為在一個小時內“拉伸”或更改圖形的 x 軸以某種方式省略每百個中的最后 40 個整數。
我嘗試了第二個方法,嘗試將每個 hhmm 值中的最后兩個字符更改為字串,并將它們乘以 (5/3) 以在一個小時內拉伸它們。這帶來了許多問題,例如不需要額外字符的地方以及從字串轉換為 int 時對 0 的錯誤處理。
我認為將資料轉換為正確的格式是最好的主意,但我什至不確定我將如何執行此操作,因為我只能看到有關如何在我沒有經驗的 SQL 中執行此操作的說明。
# 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)

# 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()
uj5u.com熱心網友回復:
對資料集中的分鐘數進行插值(在間隔0-100i/o 上傳播0-60)(保持小時數不變)產生以下圖,我認為這可以緩解您提到的“差距”問題。

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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/390056.html
標籤:Python sqlite matplotlib 特金特
