Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 1 addition & 47 deletions applied-optimization/Manifest.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions applied-optimization/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Juniper = "2ddba703-00a4-53a7-87a5-e8b9971dde84"
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"
Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
Expand Down
37 changes: 37 additions & 0 deletions include/plot_style.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
The next cell sets the look of the plots below: the course colors, quiet grey labels and no frame. Run it once. A plot picks it up when you add `course_style...` to its keywords, after a semicolon.

```{julia}
#| eval: true
#| output: false
using Plots
# The course look for plots, used like this: bar(x, y; course_style...)
course_style = (
palette=["#537E8F", "#F6B265", "#3D5369", "#A7C7C6", "#DB6B6B", "#FCCF9C"],
background_color="#FFFDFC",
fontfamily="Helvetica",
titlefontfamily="Helvetica Bold",
titlefontsize=18,
titlefontcolor="#363D45",
titlelocation=:left,
foreground_color_text="#6B7480",
foreground_color_guide="#6B7480",
foreground_color_axis="#9AA1A8",
foreground_color_border="#9AA1A8",
yforeground_color_border="#FFFDFC", # no line on the left, the grid is enough
guidefontsize=12,
tickfontsize=11,
legendfontsize=11,
foreground_color_legend=nothing,
background_color_legend=nothing,
grid=:y,
gridcolor="#E8E4E0",
gridalpha=1,
framestyle=:axes,
tick_direction=:none,
yformatter=:plain,
dpi=300,
top_margin=4Plots.mm,
left_margin=8Plots.mm,
bottom_margin=6Plots.mm
)
```
37 changes: 37 additions & 0 deletions tutorials/tutorial-03-05-Plotting.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ In this tutorial, you'll learn how to:
2. Customize your plots with colors, labels, and styles
3. Add multiple data series to a single plot
4. Save your plots as image files for use in reports or presentations
5. Give your plots a clear look that puts the data first

Follow the instructions, write your code in the designated code blocks, and validate your results with `@assert` statements.

Expand Down Expand Up @@ -284,6 +285,42 @@ display(box_plot)

---

# Section 6 - The Course Look

The plots above use the defaults of Plots.jl. They are correct, but a reader should get their point at a glance. Four rules from the [plotting lecture of our Python course](https://beyondsimulations.github.io/Introduction-to-Python/lectures/lec_09_plotting.html) get you most of the way:

- **The title states the point**: "Category D sells most" says more than "Bar Plot Example".
- **Every number axis has a label with its unit**, for example *Sales (units)*.
- **Bars start at 0**: a bar shows its number by its length, so a cut axis distorts the comparison.
- **Quiet everything that is not data**: no frame, light grid lines, grey labels, calm colors.

The later tutorials share one style for this, called `course_style`.

{{< include ../include/plot_style.qmd >}}

Here is the bar plot from above in the course look:

```{julia}
#| eval: true
#| fig-alt: "The bar plot from above in the course style: four slate-blue bars for the categories A to D with the sales 15, 23, 18 and 30 units, titled Category D sells most: 30 units. The y-axis is labeled Sales (units) and starts at 0, with light horizontal grid lines and no frame."
course_bar_plot = bar(
x_categories,
y_values;
title="Category D sells most: 30 units",
xlabel="Category",
ylabel="Sales (units)",
legend=false,
course_style...,
linecolor=course_style.background_color,
size=(800, 400)
)
display(course_bar_plot)
```

The three dots after `course_style` unpack all of its settings into the call, as if you had typed each of them. You can add more settings after it, like `size=(800, 400)` here, and if one of them is already in `course_style`, the one after it wins. `course_style.background_color` reads a single setting back, so the gaps between the bars match the background.

---

# Conclusion

Fantastic! You've completed the tutorial on basic plotting in Julia. You've learned how to create basic plots and customize and save them. Continue to the next file to learn more.
Expand Down
18 changes: 12 additions & 6 deletions tutorials/tutorial-04-03-constraints.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,25 @@ println("Model components validated successfully!")

# Visualization of Results

Let's visualize our optimal solution:
Let's visualize our optimal solution.

{{< include ../include/plot_style.qmd >}}

```{julia}
#| eval: false
using Plots
# Assuming the model has been solved!!!
optimal_production = value.(production)

bar(1:8, optimal_production,
title="Optimal Production Levels",
bar(1:8, optimal_production;
title="Optimal production levels",
xlabel="Product",
ylabel="Production Quantity",
legend=false)
ylabel="Production (units per day)",
legend=false,
course_style...,
linecolor=course_style.background_color,
xticks=1:8,
ylims=:round, # bars start at 0, the axis ends on a round number
size=(800, 400))
```

Take a close look at the chart: the bars show exactly two levels. Products 1 to 4 are produced at their demand limit of 5, and products 5 to 8 at their capacity limit of 12. Every variable sits at its tightest upper bound - what does that tell you about the constraints in this model?
Expand Down
79 changes: 35 additions & 44 deletions tutorials/tutorial-05-production.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,12 @@ using CSV
using DataFrames
using Plots
using StatsPlots
plotly() # This will create interactive plots later on
```

::: {.callout-tip}
If you haven't installed the packages yet, you can do so by running `using Pkg` first and then `Pkg.add(["JuMP", "HiGHS", "CSV", "DataFrames", "Plots", "StatsPlots"])`.
:::

::: {.callout-note}
Zed cannot show interactive plots yet. If you work in Zed, remove the `plotly()` line: your plots then appear as images.
:::

---

## Load the data
Expand Down Expand Up @@ -386,6 +381,8 @@ The following code creates production and warehouse plots for you. Use it to ver
The creation of the dataframes and the plots is implemented inside a function, as we will need to use it multiple times in the following tasks.
:::

{{< include ../include/plot_style.qmd >}}

```{julia}
#| eval: false
# Create the production results
Expand Down Expand Up @@ -418,44 +415,47 @@ function create_production_results()
return productionResults
end

# All plots in this tutorial: one bar per week, stacked by group
function weekly_bars(period, values, group; ylabel, title)
p = groupedbar(
replace.(period, "week_" => ""), # "week_01" becomes "01"
values;
group=replace.(group, "_" => " "), # "Amber_Ale" becomes "Amber Ale"
bar_position=:stack,
xlabel="Week",
ylabel=ylabel,
title=title,
course_style...,
linecolor=course_style.background_color, # a thin gap between the stacked parts
ylims=:round, # bars start at 0, the axis ends on a round number
bar_width=0.8,
legend=:outertop,
legend_column=-1,
size=(1200, 600)
)
return p
end

# Create the production plot
function create_production_plot(productionResults)
p = groupedbar(
p = weekly_bars(
productionResults.period,
productionResults.productQuantity,
group=productionResults.product,
ylabel="Production Quantity (Bottles)",
xlabel="Period",
title="Production Schedule by Beer Type",
size=(1200,600),
palette = :Set3,
legend=:outertopright,
xrotation = 45,
legendtitle="Beer Type",
bar_width=0.7,
grid=false,
dpi=300
productionResults.product;
ylabel="Production (bottles)",
title="Production schedule"
)
return p
end

# Create the warehouse stock plot
function create_warehouse_plot(productionResults)
p = groupedbar(
p = weekly_bars(
productionResults.period,
productionResults.WarehouseStockPeriodEnd,
group=productionResults.product,
ylabel="Warehouse Stock",
xlabel="Period",
title="Warehouse Stock",
size=(1200,600),
palette = :Set3,
legend=:outertopright,
xrotation = 45,
legendtitle="Beer Type",
bar_width=0.7,
grid=false,
dpi=300
productionResults.product;
ylabel="Stock at week end (bottles)",
title="Warehouse stock"
)
return p
end
Expand Down Expand Up @@ -523,21 +523,12 @@ end

# Create the cost plot
function create_cost_plot(stacked_costs)
p = groupedbar(
p = weekly_bars(
stacked_costs.period,
stacked_costs.Cost,
group=stacked_costs.Cost_Type,
round.(stacked_costs.Cost), # whole euros keep the axis from dipping below 0
stacked_costs.Cost_Type;
ylabel="Costs (€)",
xlabel="Period",
title="Setup and Inventory Costs per Period",
size=(1200,600),
palette=:Set2,
legend=:outertopright,
xrotation=45,
legendtitle="Cost Type",
bar_width=0.7,
grid=false,
dpi=300
title="Setup and inventory costs"
)
return p
end
Expand Down
Loading
Loading