# How to add diagonal line to log-log plot

**URL:** <https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [September 12, 2024, 12:17pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322 "2024-09-12T12:17:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 12, 2024, 12:17pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/1 "2024-09-12T12:17:45Z")

</div>

Hello,

I would like to plot a diagonal line on a log-log plot. How can I do that?

```julia
using Plots
x = 1:4
y = [44,29,6,1]
scatter(x, y, xaxis=:log10, yaxis=:log10)
Plots.abline!(-1, 44)

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [September 12, 2024, 2:19pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/2 "2024-09-12T14:19:44Z")

</div>

Will this work?

```julia
plot!(x -> x, 1, 50)

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 12, 2024, 2:29pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/3 "2024-09-12T14:29:47Z")

</div>

> [@mthelm85](#):
>
> `plot!(x -> x, 1, 50)`

Thanks for your reply. The identity line is not what I am looking for. I am trying to do two things. One is to plot a diagonal line (i.e., with a slope of -1 and intercept at the max of y). The other thing, which has been more difficult, is plotting the best fit linear equation. Basically, I am trying to analyze whether a network is scale free. Here is something similar to my goal:

 ![The-log-log-plot-and-dependence-of-Pk-against-k-illustrating-a-scale-free-topology-of](https://global.discourse-cdn.com/julialang/original/3X/d/a/dacf2014f7472ac17b45d2e3f440ee5eba0a6c02.png)

Among the things I tried, was LogFit in Curve Fit, but that did not work I tried LinearFit, but it was not linear in log space even with xaxis = :log10, yaxis = :log10.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [September 12, 2024, 3:17pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/4 "2024-09-12T15:17:48Z")

</div>

Your example graph is plotted in log space. Why can’t you transform your data into log space and do a simple least squares fit to the transformed data? You can then either plot the results in log space (like your example), or transform the fit data back into lin space and plot on a log/log scale like in your original code snippet.

In other words, you need to be clear whether your lin to log transformation is done in the data themselves or if is merely done in the representation of the data on the chart. You can’t mix.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [September 12, 2024, 3:33pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/5 "2024-09-12T15:33:35Z")

</div>

A simple way to add a regression line is:

```julia
scatter(log10.(x), log10.(y), smooth=true)

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 12, 2024, 3:34pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/6 "2024-09-12T15:34:37Z")

</div>

I did try something to that effect, but it didn’t work. Here is the code:

```julia
using Plots
using CurveFit
x = 1:4
y = [44,29,6,1]
log_x = log10.(x)
log_y = log10.(y)
scatter(x, y, xaxis = :log10, yaxis = :log10)
fit = curve_fit(LinearFit, log_x, log_y)
plot!(log_x, fit.(log_x))

```

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

I think the problem is that `plot!` performs another log10 transform. If I fit in real space, some of the predicted values are negative, which leads to an error.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [September 12, 2024, 3:39pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/7 "2024-09-12T15:39:21Z")

</div>

> `plot!` performs another log10 transform

Yes, because you are trying to plot on log-log axes.

Try `plot!` with `x` rather than `log_x` and transform the fit data to lin using `10^(fit_x)`. With both in linear space you can then plot them onto your original log/log axes.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 12, 2024, 3:44pm UTC](https://discourse.julialang.org/t/how-to-add-diagonal-line-to-log-log-plot/119322/8 "2024-09-12T15:44:21Z")

</div>

> [@TimG](#):
>
> Yes, because you are trying to plot on log-log axes.
> 
> Try `plot!` with `x` rather than `log_x` and transform the fit data to lin using `10^(fit_x)`. With both in linear space you can then plot them onto your original log/log axes.

I tried with `xaxis = :exp10` but that did not work. Maybe it is not an option for `xaxis`. What you proposed did work. Thank you for your help.

Here is the solution for future reference:

```julia
using Plots
using CurveFit
x = 1:4
y = [44,29,6,1]
log_x = log10.(x)
log_y = log10.(y)
scatter(x, y, xaxis = :log10, yaxis = :log10)
fit = curve_fit(LinearFit, log_x, log_y)
plot!(x, 10 .^(fit.(log_x)))

```

Thank you both for your help. I’ll mark this as the solution, but attribute it to TimG.
