在python中围绕数据绘制正方形

2024-04-26 22:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这些数据:

dict_value = {'17/11/2019': 191.9,
 '18/11/2019': 191.9,
 '19/11/2019': 191.9078431372549,
 '20/11/2019': 191.93106497500963,
 '21/11/2019': 191.9729839955975,
 '24/11/2019': 192.11914148596625,
 '25/11/2019': 192.21643005514406,
 '26/11/2019': 192.28637397455017,
 '27/11/2019': 192.35357499515604,
 '28/11/2019': 192.4259838188754,
 '1/12/2019': 192.54025896323324,
 '2/12/2019': 192.57593508232216,
 '3/12/2019': 192.60825135360366,
 '4/12/2019': 192.6157709083643,
 '5/12/2019': 192.58770146097748,
 '8/12/2019': 192.584262187998,
 '9/12/2019': 192.48291857278238,
 '10/12/2019': 192.3620198052223,
 '11/12/2019': 192.23409745991947,
 '12/12/2019': 192.09550540266773,
 '15/12/2019': 191.9701914653082,
 '16/12/2019': 191.90861532941378,
 '17/12/2019': 191.83376766943675,
 '18/12/2019': 191.89911011377257,
 '19/12/2019': 191.86385089362463,
 '22/12/2019': 191.88095478014915,
 '23/12/2019': 191.94052518092764,
 '24/12/2019': 191.99972027187167,
 '25/12/2019': 192.13071163375906,
 '26/12/2019': 192.2824484324352,
 '29/12/2019': 192.46745045469262,
 '30/12/2019': 192.46745045469262,
 '31/12/2019': 192.46745045469262,
 '1/1/2020': 192.46745045469262,
 '2/1/2020': 192.46745045469262,
 '5/1/2020': 192.46745045469262,
 '6/1/2020': 192.46745045469262,
 '7/1/2020': 192.46745045469262,
 '8/1/2020': 192.46745045469262,
 '9/1/2020': 192.46745045469262}

我想画出整件事,并画一个正方形,围绕“2019年12月10日”到“2019年12月17日”的数据


Tags: 数据valuedict正方形整件事
1条回答
网友
1楼 · 发布于 2024-04-26 22:30:26
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import sys

def main():
    dict_value = {'17/11/2019': 191.9,
    '18/11/2019': 191.9,
    '19/11/2019': 191.9078431372549,
    '20/11/2019': 191.93106497500963,
    '21/11/2019': 191.9729839955975,
    '24/11/2019': 192.11914148596625,
    '25/11/2019': 192.21643005514406,
    '26/11/2019': 192.28637397455017,
    '27/11/2019': 192.35357499515604,
    '28/11/2019': 192.4259838188754,
    '1/12/2019': 192.54025896323324,
    '2/12/2019': 192.57593508232216,
    '3/12/2019': 192.60825135360366,
    '4/12/2019': 192.6157709083643,
    '5/12/2019': 192.58770146097748,
    '8/12/2019': 192.584262187998,
    '9/12/2019': 192.48291857278238,
    '10/12/2019': 192.3620198052223,
    '11/12/2019': 192.23409745991947,
    '12/12/2019': 192.09550540266773,
    '15/12/2019': 191.9701914653082,
    '16/12/2019': 191.90861532941378,
    '17/12/2019': 191.83376766943675,
    '18/12/2019': 191.89911011377257,
    '19/12/2019': 191.86385089362463,
    '22/12/2019': 191.88095478014915,
    '23/12/2019': 191.94052518092764,
    '24/12/2019': 191.99972027187167,
    '25/12/2019': 192.13071163375906,
    '26/12/2019': 192.2824484324352,
    '29/12/2019': 192.46745045469262,
    '30/12/2019': 192.46745045469262,
    '31/12/2019': 192.46745045469262,
    '1/1/2020': 192.46745045469262,
    '2/1/2020': 192.46745045469262,
    '5/1/2020': 192.46745045469262,
    '6/1/2020': 192.46745045469262,
    '7/1/2020': 192.46745045469262,
    '8/1/2020': 192.46745045469262,
    '9/1/2020': 192.46745045469262}

x = []
y = []

for pair in dict_value:
    x.append(pair)
    y.append(dict_value.get(pair))


fig, ax = plt.subplots()
plt.plot(x, y)
plt.xticks(x, rotation='vertical')
ax.set(xlabel='val', ylabel='date', title="title")
ax.grid()

ax.add_patch(
    patches.Rectangle(
        xy=('10/12/2019', min(y) - 0.025),  # point of origin.
        width='17/12/2019',
        height= max(y) - min(y) - 0.075,
        linewidth=1,
        color='red',
        fill=False
    )
)

plt.show()

这就是你要找的吗

相关问题 更多 >