Python Forum
Having trouble setting the X-Ticks for a data set I am working with.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having trouble setting the X-Ticks for a data set I am working with.
#1
I am using a large data set that, it contains homelessness numbers in NYC from 08/21/2013 to 01/16/2024 and when I graph it normally it gives a unreadable mess due to the amount of dates. I tried using various things to remove most of the dates such as "set_xticks" and others but I keep getting this error message:
"'DataFrame' object has no attribute 'set_xticks'"

my code is the following:
"plt.figure(figsize=(10,10))
sns.lineplot(data=homelessness_data, x='Date of Census', y='Total Individuals in Shelter')
homelessness_data.set_xticks(['01/01/2013', '01/01/2014', '01/01/2015', '01/01/2016', '01/01/2017', '01/01/2018','01/01/2019', '01/01/2020', '01/01/2021','01/01/2022', '01/01/2023' ])" I have read through multiple stack overflow stuff but it still isnt working.
can anyone help?
I attached the CSV im using in case that helps. I can also answer any following questions one may have.
Larz60+ write Jan-22-2024, 10:37 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.csv   DHS_Homeless_Shelter_Census.csv (Size: 123.72 KB / Downloads: 1)
Reply
#2
My guess is that Date of Census in your homelessness_data is not a datetime or date object. I read your data like this:
import pandas as pd

data = pd.read_csv("census.csv")
print(data.dtypes)
Output:
Date of Census object Total Individuals in Shelter int64 Total Single Adults in Shelter int64 Families with Children in Shelter int64 Adult Families in Shelter int64 dtype: object
Notice that Date of Census is an "object", which is a string. You need Date of Census to be a date, then seaborn/matplotlib can create ticks that are reasonable in count and distribution because it will know what the tics mean.

Compare the plot produced by this code:
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("census.csv")
data.plot(x="Date of Census", y="Total Individuals in Shelter")
plt.show()
With the plot produced by this code.
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv("census.csv", parse_dates=["Date of Census"])
data.plot(x="Date of Census", y="Total Individuals in Shelter")
plt.show()
Matplotlib usually does a good job making tic marks and labels. When something goes wrong it is usually because you are not providing enough information for matplotlib to make good decisions.

I think this will fix your problem, but if you do want to set labels and the like (which is almost always a bad idea), you do this using the Axis object returned by sns.lineplot()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing data between functions, got it working, but need clarification please digitalmatic7 4 16,011 Apr-18-2018, 09:30 AM
Last Post: digitalmatic7
  I'm working onn below code to extract data from excel using python kiran 1 3,304 Oct-24-2017, 01:42 PM
Last Post: kiran

Forum Jump:

User Panel Messages

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