I have a histogram and I want to plot some text on it, which is coming from a dictionary.
Once I have made the histogram using
using Plots
histogram(data)
I can put in text by using the command
annotate!(xpos, ypos, text("mytext", :red, :right, 3))
and this works fine. However if I want to loop through a dictionary (of unknown size) and add the values to the plot, something like
for k in keys(MyDict)
annotate!(xpos, ypos, text(MyDict[k], :red, :right, 3))
end
does nothing. I can manually do it key by key, but why can't I put it in a for loop? I assume that it has something to do with the scope of the for loop. Is there any work around?
I assume you are working in a Jupyter notebook, at least this is the only scenario I can imagine to make sense of your observations
;)One important thing to realise (and this is independent of Julia, it's just the way Jupyter, or any other REPL usually works) is that Jupyter tries to visualise the input of the cell by taking the output of the cell, which is essentially what the last expression in the cell returns. This might sound confusing, so let me show an example based on yours.
You see three cells below:
using Plots-> does not return anything worth to visualisedata = rand(100)-> Jupyter will show you the first and last few elementshistogram(data)-> the output will be rendered as an imageNow coming back to your problem: if you do
histogram(data), you will be presented a histogram, just like in the example above, sincehistogram(data)returns something with Jupyter can display as an image.If you use
annotate!(...), it will act on the last plot you created (which is your histogram) and it will re-return it which Jupyter can display, so you'll see the annotated histogram:Now coming to the for-loop: they do not return anything, so Jupyter will not be able to display anything
What you want is: creating a variable to access your initial histogram, called e.g.
fig:notice that an assignment in Julia returns the right-hand side, so that Jupyter displays the histogram in this case.
Now you can use whatever mutating function (like
annotate!()) in as many for-loops as you want and then just give Jupyter thefigobject to display:Note that if you are not in a Jupyter environment, you can use
show(fig)to display it, or better:savefig(fig, "filename.png")(or whatever image extension you prefer) to save the plot.