I'm dealing with a dataframe of size (579x31), and I want to plot all columns except the :date
column and modify the plot to change the line width of the last 30 data points in the plot.
using DataFrames
using Plots
using StatsPlots
# `selection` is a subset of a dataframe object.
p = @df selection plot(
:date, cols(2:31),
legend=:outerright,
xlabel="Date",
ylabel="Close",
title="Stock Prices",
size=(1000, 600),
dpi=300,
left_margin=5Plots.mm,
bottom_margin=5Plots.mm,
legend_title="DJI Stocks",
)
This gives me the following plot:
Now I modify the line width of the last 30 data points:
# Grab the default colors to prevent color changes in the modified plot
colors = [p.series_list[i].plotattributes[:linecolor] for i in eachindex(p.series_list)];
# Modify the plot
@df selection[end-30:end, :] plot!(
:date, cols(2:31),
linewidth=2,
label=nothing,
linecolors=colors
)
The problem is where it sets the color of each data point rather than each line (plot!):
(I used a magnifier to better see the problem)
How can I fix this?
The expected output is a plot similar to the first figure (p
) with thicker lines in the last 30 data points.
Minimal reproducible example:
using DataFrames
using Plots
using StatsPlots
using Dates
start_date = Date("2021-01-03")
end_date = start_date + Day(4)
df = DataFrame(
date = start_date:Day(1):end_date,
col1 = rand(5),
col2 = rand(5),
col3 = rand(5),
col4 = rand(5)
)
p = @df df plot(
:date, cols(2:5),
legend=:outerright,
xlabel="Date",
ylabel="Close",
title="Stock Prices",
size=(1000, 600),
dpi=300,
left_margin=5Plots.mm,
bottom_margin=5Plots.mm,
legend_title="DJI Stocks",
)
colors = [p.series_list[i].plotattributes[:linecolor] for i in eachindex(p.series_list)];
@df df[end-2:end, :] plot!(
:date, cols(2:5),
linewidth=2,
label=nothing,
linecolors=colors
)