Why does not Seaborn Relplot print datetime value on x-axis? - python

I'm trying to solve a Kaggle Competition to get deeper into data science knowledge. I'm dealing with an issue with seaborn library. I'm trying to plot a distribution of a feature along the date but the relplot function is not able to print the datetime value. On the output, I see a big black box instead of values.
Here there is my code, for plotting:
rainfall_types = list(auser.loc[:,1:])
grid = sns.relplot(x='Date', y=rainfall_types[0], kind="line", data=auser);
grid.fig.autofmt_xdate()
Here there is the
Seaborn.relpot output and the head of my dataset

I found the error. Pratically, when you use pandas.read_csv(dataset), if your dataset contains datetime column they are parsed as object, but python read these values as 'str' (string). So when you are going to plot them, matplotlib is not able to show them correctly.
To avoid this behaviour, you should convert the datetime value into datetime object by using:
df = pandas.read_csv(dataset, parse_date='Column_Date')
In this way, we are going to indicate to pandas library that there is a date column identified by the key 'Column_Date' and it has to be converted into datetime object.
If you want, you could use the Column Date as index for your dataframe, to speed up the analyis along the time. To do it add argument index='Column_Date' at your read_csv.
I hope you will find it helpful.

Related

Find the format that is visualized in a dataframe from a datetime type column

I have a problem, that I am not able to automatically solve since I just cannot find how to do it. I would like to extract the format of a datetime column that is visualized when a dataframe is printed.
I have a column within my dataframe that is of the type datetime.datetime. If I print the dataframe I get the following:
And if I print one value I get this:
I am not sure what the approach is to easily return the format of the values in the upper image. Just to be clear, I would like to have code that will return the format, that is shown in the dataframe, in datetime codes. In this example it should return: '%Y-%m-%d %H:%M:%S.%f'.
I am able to return this by first transforming the column to string values and then use the function _guess_datetime_format_for_array() from pandas.core.tools.datetimes, but this approach is a bit excessive in my opinion. Does anyone have a suggestion of a more easy solution?

How do I display a timedelta in the format HH:MM:SS on my Plotly Express line chart's axis?

I want to plot a continuous 'Time' column against dates on a simple timeseries linechart in plotly express. The 'Time' column starts out as a string, in the format HH:MM:SS, but when I plot this outright it is treated as discrete values. To remedy this and make it continuous I tried converting to timedelta data type, using pd.to_timedelta. This correctly converts my column into nanoseconds and the shape of the line and axis looks correct. However I do not want to display the axis as nanoseconds, or any other fixed unit, I would like it to display as HH:MM:SS, but am unsure how I might format this.
There is no easy way to do this in express. But if you use Plotly go you can use this piece of code directly from their website.
fig.update_xaxes(
ticktext=["End of Q1", "End of Q2", "End of Q3", "End of Q4"],
tickvals=["2016-04-01", "2016-07-01", "2016-10-01"],
)
It will map ticket vals display name to the matching entry in ticket text. This should maintain scale if tickvals are a scalar. In addition in this example you can have these pieces of text loop every year after.
Here is the link to their website: Plotly Axes with Labels

Plotting multiple time series from pandas dataframe

I have a pandas dataframe loaded from file in the following format:
ID,Date,Time,Value1,Value2,Value3,Value4
0063,04/21/2020,11:22:55,0.0347,0.41,1440,10.5
0064,04/21/2020,11:22:56,0.0355,0.41,1440,10.4
...
9849,04/22/2020,10:46:19,0.058,1.05,1460,10.6
I have tried multiple methods of plotting a line graph of each value vs date/time or a single graph with multiple subplots with limited success. I am hoping someone with much more experience may have an elegant solution to try as opposed to my blind swinging. Note that the dataset may have large breaks in time between days.
Thanks!
parsing dates during the import of the pandas dataframe seemed to be my biggest issue. Once I added parse_dates to the pd.read_csv I was able to define the dt column and plot with matplotlib as expected.
df = pd.read_csv(input_text, parse_dates = [["Date", "Time"]])
dt = df["Date_Time"]

Using DateTimeIndex for set_xlim on matplotlib

How can I set x/y limits on matplotlib to certain datetime values?
I got a DateTimeIndex object (called time) and i want the plots to fit inside the first and last value of this index.
If I try ax.set_xlim(time[0],time[-1])
it throws me this error:
Cannot compare type Timedelta with type float
Any suggestions?
The time handling in matplotlib is gregorian, so it needs to be converted, I think it needs to be done with date2num().
matplotlib API Overview: Dates
ax.set_xlim(date2num([series_.index.min(), series_.index.max()]))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))

Changing x-axis of a datetime e.g trimming a graph with matplotlib in python

I have an excel sheet of retail gas prices from years 1990 to 2019. I successfully plotted a graph of their prices against the date(years). The x-axis was created on its own and its scaled to jump every 4 years. The Date is a datetime type and the Gas price is a float.
my plot was created by writing:
date = dataset['Date']
price = dataset['U.S. All Grades All Formulations Retail Gasoline Prices (Dollars per Gallon)']
plt.plot_date(date, price, linestyle='solid')
plt.xlabel("Date")
plt.ylabel("Gaslone Price / Dollar Per Gallon")
plt.tight_layout()
Now I would to "zoom" into the picture and create another graph but I would like that part where there is a steep decline from around years 2007 to 2009.
I tried using plt.xlim but I'm not sure how to input my limits.
Thank you.
Coming to this waaaaaay after the fact, but since I just ran into this in the top of Google results while trying to do the same, I'll go ahead and answer. Assuming that your dates are of datatype DateTime (it looks like they are), you should be able to use the following:
plt.xlim(pd.to_datetime('2007-01-01'), pd.to_datetime('2009-12-31'))
The xlim() function takes two arguments, a left and a right boundary. These can be placed in a tuple (inside parenthesis), but even if you don't include them in a tuple, it seems that matplotlib will still figure it out. The use of pd.to_datetime is necessary to convert the date strings into a datetime object that can be checked against the datetime objects being used on the x-axis, so pyplot can identify where to draw the left and right boundaries. Without this conversion, pyplot would crash with an IndexError, because it would be trying to match a string to a non-string item (the DateTime x-axis objects).
In a scenario where you weren't needing to convert to datetime objects, you could simply pass values into the xlim() function without the conversion to datetime.

Categories

Resources