# PlotlyJS: how to introduce shapes into time series?

**URL:** <https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [August 6, 2021, 4:46pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955 "2021-08-06T16:46:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 6, 2021, 4:46pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/1 "2021-08-06T16:46:09Z")

</div>

Hi, I need to insert shapes into a time series using PlotlyJS, and name them. I know how to do it using Plots or PGFPlotsX. For example, what I want is something similar to the following plot, produced with PGFPlotsX after some raw LaTeX code passed into the script:

 ![a](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac55ed688140ea1e93a4441f2013e055d22e15a9.png)

This material is for teaching, and the combination of Pluto and PlotlyJS looks perfect due to the interactivity that we may get even in static HTML files. I had no problem replicating the example that is available in the PlotlyJS documentation:

 ![aa](https://global.discourse-cdn.com/julialang/original/3X/6/1/6178dc2709993ca6114a999531031d2ee122b735.png)

Unfortunately, I have failed to accomplish this particular need in PlotlyJS, even in the more archaic version of my demands (just the shapes). Nevertheless, I also need to name the different shapes (as in the first plot above). My example below is not working as intended because I am not correctly passing the type Date array into shapes, and after many twists, I still do not know how to do it.

MWE:

```julia
using PlotlyJS
using Dates
using MonthlyDates

```

My useless efforts surrounded this piece of code:

```julia
period10_5 = QuarterlyDate(1960, 1):Quarter(1):QuarterlyDate(2021,1)
y1 = randn(length(period10_5))
y2 = rand(length(period10_5))
#z = period10_5
	
trace00 = scatter(;x = Date.(period10_5), y = y1, 
		     name = "Shrek", mode="markers+lines",
		     marker_symbol = "circle", marker_size = "5", 
             line_width = 0.3 , marker_color = "Blue")
	
trace01 = scatter(;x = Date.(period10_5), y = y2, 
			 name = "Murphy", mode = "markers+lines",
			 marker_symbol = "circle", marker_size = "5", line_width = 0.3, 
             marker_color = "Red")
	
shapes = rect(["period10_5[50]", "period10_5[70]"], ["period10_5[120]", "period10_5[150]"],
             0, 1 ; fillcolor = "red", opacity = 0.2, line_width = 0,
             xref = period10_5, yref = "paper")
	
layout00 = Layout(;title_text = "Shapes, my shapes", title_x = 0.5,

			   shapes=shapes,
		
               xaxis = attr(
                	title = "Quarterly obervations",
                	tickformat = "%Y",
                	hoverformat = "%Y-Q%q",
                	tick0 = "1960/01/01",
                	dtick = "M120"),
		
        	    #xaxis_range = [1960, 2020],
        	    yaxis_title = "Percentage points",
        	    #yaxis_range=[-2, 2], 
        	    titlefont_size = 16)
	
plot([trace00, trace01], layout00)

```

This produces the following plot, but I am unable to get the shapes.

 ![aaa](https://global.discourse-cdn.com/julialang/original/3X/a/0/a053dd8af94a4f2e1865440bc1be35a6106c833f.png)

Some help will be very much appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [August 6, 2021, 6:31pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/2 "2021-08-06T18:31:00Z")

</div>

Your rectangle coordinates `x0= "period10_5[50]", x1="period10_5[70]"`, are strings with no significance for plotly.js. They cannot be converted to the corresponding Date.

To get plotted your shapes, replace these strings by `Date(period10_5[50])`, etc.  
But in this case the function `rect` doesn’t work and the two shapes are overlayed.

Instead,use the default definition of a rectangle [https://plotly.com/javascript/reference/layout/shapes/](https://plotly.com/javascript/reference/layout/shapes/),  
i.e. replace your shape definition with:

```julia
common_attr = (fillcolor = "red", opacity = 0.2, line_width = 0,
               xref = period10_5, yref = "paper" )
shapes =[attr(type="rect", x0=Date(period10_5[50]), y0=0, x1= Date(period10_5[70]), y1=1; common_attr..., ), 
         attr(type="rect", x0=Date(period10_5[120]), y0=0, x1= Date(period10_5[150]), y1=1; common_attr...,)]

```

![shapes](https://global.discourse-cdn.com/julialang/original/3X/6/e/6efd2b8b7f7133608f4297870aeb38db397e992e.png)

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 6, 2021, 10:51pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/3 "2021-08-06T22:51:47Z")

</div>

Hi @empet, thank you very much for your quick reply. I could never get close to your solution, no way. It works very well.

In the meantime, I was desperately trying to come up with a way of solving the problem. I found a workaround that works as well, certainly a bit clumsy. It takes one more step than in your code. I have to covert an array of Date types (named as “Quarters”) in “my\_df” into an array of strings:

```julia
my_strings = my_df[!,:Quarters] = string.(my_df[:,:Quarters])

```

Then I applied the same logic in the docs’s example, using the strings to define the rectangles I want.

```julia
my_strings = my_df[!,:Quarters] = string.(my_df[:,:Quarters]) # Date type into strings
period10_5 = QuarterlyDate(1960, 1):Quarter(1):QuarterlyDate(2021,1)
y1 = randn(length(period10_5))
y2 = rand(length(period10_5))
	
trace00 = scatter(;x = Date.(period10_5), y = y1, 
		      name = "Shrek", mode="markers+lines",
		      marker_symbol="circle", marker_size="5",line_width= 0.3, 
              marker_color = "Blue")
	
trace01 = scatter(;x = Date.(period10_5), y = y2, 
		      name = "Murphy", mode="markers+lines",
		      marker_symbol="circle", marker_size="5",line_width= 0.3, 
              marker_color = "Red")
	
shapes = rect(["1962-07-01", "2010-07-01"], ["1970-01-01", "2018-01-01"],
               0, 1; fillcolor = "gray", opacity = 0.2, line_width = 0,
               xref = my_strings, yref = "paper")
	
layout00 = Layout(; title_text = "Shapes, my shapes", title_x = 0.5,

		       shapes = shapes,
		
		       annotations = [attr(x = "1966-07-01", y = 2.5, showarrow = false,
				                     text = "QE1"), 
			                  attr(x = "2014-03-01", y = 2.5, showarrow = false, 
				                     text = "QE2") ],
		
            	xaxis = attr(
                	title = "Quarterly obervations",
                	tickformat = "%Y",
                	hoverformat = "%Y-Q%q",
                	tick0 = "1960/01/01",
                	dtick = "M120"),
		
                 #xaxis_range = [1960, 2020],
        	     yaxis_title = "Percentage points",
        	     #yaxis_range=[-2, 2], 
        	     titlefont_size = 16)
	
plot([trace00, trace01], layout00)

```

The plot looks like this:

 ![aaaa](https://global.discourse-cdn.com/julialang/original/3X/4/4/449b3677640245f70fa4a7d7bf08124d2e8d5d55.png)

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 6, 2021, 11:02pm UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/4 "2021-08-06T23:02:11Z")

</div>

Sorry for one aside topic. Do you know how can I pass the following information available in the plotylyjs documentation into PlotlyJS:

```julia
textfont: {
    family: 'sans serif',
    size: 18,
    color: '#ff7f0e'
  },

```

I tried this, but it does not work (with many variations on the empty space in “sans serif”)::

```julia
textfont = [family = "sans serif", size = 18, color = "#ff7f0e"],

```

For the issue above, the size of the textfont is not relevant. But sometimes, I need a larger font, and I do not know if it is possible to insert it into PlotlyJS.

Thanks.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [August 7, 2021, 12:14am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/5 "2021-08-07T00:14:19Z")

</div>

`textfont` is a Dict:

```julia
textfont = Dict{Symbol, Any}(:family=>"sans serif",
                             :size=>18,
                             :color=>"#ff7f0e")

```

but inside a trace definition you can give it as an `attr` (attribute):

```julia
tr = scatter(x=1:5, y=rand(3:9, 5), mode="markers+text",
             text = ["Aaaa", "Bbbbbb", "Ccc", "abccc", "lmnop"],
             textfont =attr(family="sans serif",
                            size=18,
                            color="#ff7f0e"),
             textposition="top")
pl = plot(tr, Layout(width=600, height=500))

```

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 7, 2021, 12:25am UTC](https://discourse.julialang.org/t/plotlyjs-how-to-introduce-shapes-into-time-series/65955/6 "2021-08-07T00:25:27Z")

</div>

@empet, Wonderful. Thank you very much.
