-2

so i have this data:

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}

i would like to plot the entire thing and draw a square around data from '10/12/2019' to '17/12/2019'

Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
M.I
  • 379
  • 3
  • 18
  • What have you tried so far? – Thomas Schillaci Mar 02 '20 at 08:13
  • Does this answer your question? [How to draw a rectangle over a specific region in a matplotlib graph](https://stackoverflow.com/questions/13013781/how-to-draw-a-rectangle-over-a-specific-region-in-a-matplotlib-graph) – Diziet Asahi Mar 02 '20 at 08:40
  • @DizietAsahi it doesn't work when i substitute the x and y coordinate it gives me this error: TypeError: unsupported operand type(s) for -: 'str' and 'float' – M.I Mar 02 '20 at 08:49

1 Answers1

1
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()

is this what you are looking for?

Connor
  • 26
  • 1