Python Forum
matplotlib x-axis wrong order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib x-axis wrong order
#1
Greetings!

I would like to create a simple 2D plot of 270 data points.
batchnrs = list(range(0,135))*2
plt.plot(batchnrs,  loss_per_batch, label="loss")
plt.show()
However, the x-axis in the plot does not go 0,1,...,134,0,1,...,134 (as I would like it to do) but only from 0 to 134 and the second half of y elements is simply plotted right across the first one. I would like to plot my data points as if the x-axis went from 0 to 269, so I just need to change what is portrayed on the x-ticks.


Best,
SL
Reply
#2
Why not set the axis to 0..269? That will give you what you want. What you are seeing, with the second line starting at zero, is what I would expect to happen.
Reply
#3
Why not let the data drive the labels?
batchnrs = list(range(len(loss_per_batch)))
plt.plot(batchnrs,  loss_per_batch, label="loss")
plt.show()
Reply
#4
Thanks guys!

The thing is that the second half of my batches are batches from a following epoch. Earlier in the document, they were labelled from 0 to 134 again, which is why I would like my x-axis to be like 0,1,2,...,134,0,1,2,.... But there might be no straight forward way to do this..
Reply
#5
Maybe someone more familiar with matplotlib/pyplot will chime in, but I don't think you can have the same value in multiple places along an axis. Is it possible to give the second epoch unique labels?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 977 May-31-2023, 04:28 AM
Last Post: jacklee26
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,535 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  x-axis labels with Matplotlib Mark17 8 2,194 Mar-23-2022, 06:10 PM
Last Post: Mark17
  How to make x-axis readable with matplotlib Mark17 7 3,909 Mar-01-2022, 04:30 PM
Last Post: DPaul
  matplotlib x axis range goes over the set range Pedroski55 5 3,176 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,309 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,780 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,221 Mar-11-2021, 10:52 AM
Last Post: buran
  Help with Matplotlib and ordering the axis theliberalguy97 3 7,974 Dec-12-2020, 08:06 PM
Last Post: deanhystad
  How to groupby Months showing average order value - Pandas & matplotlib Rwood90 0 1,850 Oct-20-2020, 12:53 PM
Last Post: Rwood90

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020