Python Forum
How to customize x axis in matplotlib.pyplot for a scatter plot?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to customize x axis in matplotlib.pyplot for a scatter plot?
#1
I have a list of 8698 temperatures which I want to plot on a scatter plot. How do I set the x-axis ticks labeled to a list of years? The list of years I have isn't as long as the temperature list, so I'm getting the ValueError: x and y must be the same size
Reply
#2
tell us a bit about your code, environment, etc.
Reply
#3
I only have 3 lists.
One is temp_list, which contains 8698 temperatures.
The second one is date_list, which 8698 dates in the format of m/d/y.
The last one is simply 'x', and it contains the number 1 up to 8698, which I'm currently using as the x axis.

Currently, I'm creating a scatter plot as follows
import matplotlib.pyplot as plt
plt.scatter(x, temp_list)
plt.show()
This creates a scatter plot, but the x axis has ticks at 0, 2000, 4000, 6000, and 8000.
How do I change these numbers to 1996, 2001, 2006, 2011, 2016?
Reply
#4
see tutorial here: https://matplotlib.org/tutorials/introdu...-pyplot-py
Reply
#5
Or this, which I think directly addresses setting an axis to a date range:

https://matplotlib.org/gallery/text_labe.../date.html
Reply
#6
Maybe I wasn't clear enough.
[Image: WBAWg9G]

I want the plot to look exactly the same, just 0 to read 1996, 2000 to read 2001, 4000 to read 2006, 6000 to read 2011, and 8000 to read 2016.
I couldn't see how to do that in either of the links posted, sorry if I missed it.
Reply
#7
Can't promise anything, but you could post a truncated version of your data (maybe 10 points or so) so we can see exactly what it looks like.
Reply
#8
It's just a list of floats
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
temp_list=[52.2, 38.5, 42.0, 41.2, 37.2, 52.1, 51.8, 51.0, 62.4, 68.7, 72.4, 71.6, 60.5, 53.3, 51.3, 56.1, 62.8, 65.4, 48.6]
plt.scatter(x, temp_list)
# each x value refers to the days since 1996
Reply
#9
Your first step would probably be to convert your date list from the current format, to a Python supported date format. Once you have that, then plotting the date list should look like the link I posted. That will associate your data with a real date, and you can go from there. Right now, Python or Matplotlib doesn't recognize your data as a date.
Reply
#10
Figured it out...
I just divided every value by 365
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib : Raster Plot adithyakrish 1 3,662 May-28-2019, 05:04 AM
Last Post: heiner55
  Issues with matplotlib.pyplot schniefen 2 2,251 Apr-19-2019, 11:51 AM
Last Post: schniefen
  How to create custom error bars in matplotlib.pyplot? wlsa 1 4,214 Nov-04-2018, 09:49 PM
Last Post: wlsa
  How to plot date series in matplotlib? StrybolData 2 8,302 Jan-25-2018, 07:13 PM
Last Post: StrybolData
  Pyplot line color and spacing/padding metalray 0 2,674 May-26-2017, 08:39 AM
Last Post: metalray
  matplotlib legend and x axis interval metalray 5 25,576 Apr-14-2017, 12:07 PM
Last Post: metalray

Forum Jump:

User Panel Messages

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