# Plotting two lines on one plot over the same x range but with different x arrays in Plots.jl

**URL:** <https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198>\
**Category:** New to Julia\
**Created:** [October 22, 2019, 10:52pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198 "2019-10-22T22:52:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [October 22, 2019, 10:52pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/1 "2019-10-22T22:52:37Z")

</div>

I would like to plot two line plots on the same plot, however I have two sets of data and while for both sets the x domain range is the same (0,.25) the values which are paired with my data are different. Is there a way to put two plots together if they have the same x range but different specific x values?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 23, 2019, 2:10am UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/2 "2019-10-23T02:10:58Z")

</div>

There shoudn’t be any problem:

```julia
using Plots
x1 = collect(1:3:12)
y1 = rand(4)
x2 = collect(1:4:12)
y2= rand(3)
plot(x1,y1)
plot!(x2,y2)

```

Edit: indeed, it doesn’t work…  
Edit2: sure it works… with plot!(.)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 23, 2019, 2:38am UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/3 "2019-10-23T02:38:10Z")

</div>

do you mean `plot!(x2,y2)`

```julia
julia> scatter(x1,y1); scatter!(x2, y2)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/9/190d8493cb91ba051624b40517c1501c2a1fbea6.png)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 23, 2019, 4:43am UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/4 "2019-10-23T04:43:08Z")

</div>

> [@sylvaticus](#):
>
> using Plots x1 = collect(1:3:12) y1 = rand(4) x2 = collect(1:4:12) y2= rand(3) plot(x1,y1) !plot(x2,y2)

This works for me:

```julia
using Plots
x1 = collect(1:3:12)
y1 = rand(4)
x2 = collect(1:4:12)
y2= rand(3)
plot([x1,x2],[y1,y2])

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 23, 2019, 4:44am UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/5 "2019-10-23T04:44:18Z")

</div>

Yours works for me on Julia 1.3 rc4 with GR backend.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 23, 2019, 8:38am UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/6 "2019-10-23T08:38:49Z")

</div>

Sure… typed it with my mobile during the night after making asleep a crying baby haha…

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [October 23, 2019, 5:52pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/7 "2019-10-23T17:52:11Z")

</div>

Thank you! My problem, for the record, was I was trying to re-label the axes and plot title in the plot!() command (with the same labels), which is nonsensical because they are already specified.

---

<div class="post-metadata">

**Author:** ![CPPhysics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpphysics/32/4372_2.png) [@CPPhysics](https://discourse.julialang.org/u/CPPhysics)\
**Post date:** [October 23, 2019, 5:55pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/8 "2019-10-23T17:55:22Z")

</div>

I am now not being able to get `plot!(x1,y2, label = ["y2 label"])` to work to label the second line plot. What command can I use for this? Sorry, I am sure this is in the documentation but I could not find it.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 23, 2019, 7:31pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/9 "2019-10-23T19:31:00Z")

</div>

Not 100% what the problem is. With `using Plots` and `pyplot()` backend, I create some data (using functions):

```julia
x1 = range(5,15,length=20)
x2 = range(10,20,length=100)
f1 = x-> exp(-(x-10)^2/5^2)
f2 = x-> sin(2pi*(x-15))/(2pi*(x-15))
y1 = f1.(x1)
y2 = f2.(x2);

```

Plots are produced by:

```julia
plot(x1,y1,label="f1")
plot!(x2,y2,label="f2")
plot!(xlim=(0,25))

```

giving:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27276952925909b68edf288d1a2080079d42650b.png)  
Is it something like this that you want?

---

<div class="post-metadata">

**Author:** ![anon20242138](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@anon20242138](https://discourse.julialang.org/u/anon20242138)\
**Post date:** [May 19, 2021, 1:55pm UTC](https://discourse.julialang.org/t/plotting-two-lines-on-one-plot-over-the-same-x-range-but-with-different-x-arrays-in-plots-jl/30198/10 "2021-05-19T13:55:47Z")

</div>

Appreciate u men!
