Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to customize x axis in matplotlib.pyplot for a scatter plot?
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
tell us a bit about your code, environment, etc.
wlsa likes this post
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)
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?
see tutorial here:
wlsa likes this post
Or this, which I think directly addresses setting an axis to a date range:
wlsa likes this post
Maybe I wasn't clear enough.

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.
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.
wlsa likes this post
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
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.
wlsa likes this post
Figured it out...
I just divided every value by 365

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to create a new plot in a figure right below the first one after click event? codexx 0 28 Yesterday, 04:45 PM
Last Post: codexx
  Fitting xticks on x-axis schniefen 0 67 May-08-2019, 12:04 PM
Last Post: schniefen
  Issues with matplotlib.pyplot schniefen 2 130 Apr-19-2019, 11:51 AM
Last Post: schniefen
  How do I make my plot show up? ThomasM4 0 267 Dec-09-2018, 05:28 AM
Last Post: ThomasM4
  Tips on surface plot HW question Cwcox 1 331 Nov-14-2018, 11:45 PM
Last Post: Larz60+
  How to create custom error bars in matplotlib.pyplot? wlsa 1 463 Nov-04-2018, 09:49 PM
Last Post: wlsa
  Adding text to plot Pythcoronas 1 404 Sep-13-2018, 12:27 PM
Last Post: baby_quant
  plot the mean in a bar diagram sussii 3 537 May-23-2018, 11:27 PM
Last Post: wavic
  How to plot date series in matplotlib? StrybolData 2 2,301 Jan-25-2018, 07:13 PM
Last Post: StrybolData
  how to solve this problem. IndexError: axis 2 out of bounds [0, 2) bedio 10 2,412 Nov-21-2017, 02:47 PM
Last Post: heiner55

Forum Jump:

Users browsing this thread: 1 Guest(s)