How to set datetime xlim in seaborn - python
I have a dataframe:
df = pd.DataFrame({"max_cr_date":{"0":1569115380000,"1":1569115500000,"2":1569115560000,"3":1569115620000,"4":1569115680000,"5":1569115740000,"6":1569115800000,"7":1569115860000,"8":1569115920000,"9":1569115980000,"10":1569116040000,"11":1569116100000,"12":1569116160000,"13":1569116220000,"14":1569130800000,"15":1569130800000,"16":1569130800000,"17":1569130800000,"18":1569130860000,"19":1569130860000,"20":1569130860000,"21":1569130860000,"22":1569131100000,"23":1569131100000,"24":1569131160000,"25":1569131160000,"26":1569131220000,"27":1569131220000,"28":1569131280000,"29":1569131280000,"30":1569131340000,"31":1569131340000,"32":1569131400000,"33":1569131400000,"34":1569131460000,"35":1569131460000,"36":1569131520000,"37":1569131520000,"38":1569131580000,"39":1569131580000,"40":1569131640000,"41":1569131640000,"42":1569131700000,"43":1569131700000},"cnt":{"0":14,"1":14,"2":14,"3":14,"4":14,"5":14,"6":14,"7":14,"8":14,"9":14,"10":14,"11":14,"12":14,"13":14,"14":11,"15":12,"16":13,"17":14,"18":11,"19":12,"20":13,"21":14,"22":11,"23":12,"24":11,"25":12,"26":11,"27":12,"28":11,"29":12,"30":11,"31":12,"32":11,"33":12,"34":11,"35":12,"36":11,"37":12,"38":11,"39":12,"40":11,"41":12,"42":11,"43":12},"uuid":{"0":80,"1":66,"2":70,"3":80,"4":72,"5":110,"6":358,"7":123,"8":110,"9":123,"10":96,"11":89,"12":83,"13":58,"14":7,"15":28,"16":9,"17":5,"18":129,"19":116,"20":266,"21":87,"22":57,"23":86,"24":99,"25":36,"26":89,"27":30,"28":88,"29":18,"30":75,"31":26,"32":94,"33":29,"34":81,"35":32,"36":64,"37":19,"38":74,"39":26,"40":77,"41":17,"42":51,"43":21}})
df.max_cr_date = pd.to_datetime(df.max_cr_date, unit='ms')
df
df.max_cr_date.agg(['min', 'max'])
min 2019-09-22 01:23:00
max 2019-09-22 05:55:00
Name: max_cr_date, dtype: datetime64[ns]
When I try to plot the dataframe using seaborn, I get wrong xlim. For example, max_cr_date range is from 2019-09-22 01:23:00 to 2019-09-22 05:55:00, but on graph you can see year 2000, 2004...
How to set xlim to min/max of the max_cr_date column?
Regards.
You can do in this way:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
df.max_cr_date = pd.to_datetime(df.max_cr_date, unit='ms')
ax = sns.scatterplot(data=df, x="max_cr_date", y="uuid", hue='cnt', palette="vlag")
ax.set_xlim(df['max_cr_date'].min(), df['max_cr_date'].max())
myFmt = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(myFmt)
for item in ax.get_xticklabels():
item.set_rotation(45)
plt.show()
Related
Axis's Plot Show Wrong Output
I have this code: import pandas as pd import matplotlib.pyplot as plt %matplotlib inline import seaborn as sns sns.set() import yfinance as yf df = yf.download('AAPL', start='2001-01-01', end='2005-12-31', progress=False) df.head() df = df.reset_index() df['Date'] = pd.to_datetime(df.Date, format='%Y%m%d') df.dropna(how='any', inplace=True) # Plot the returns plt.figure(figsize=(10,6)) plt.grid(True) plt.xlabel('Dates') plt.ylabel('Prices') plt.plot(df['Close']) plt.title('Close Price', fontsize=16) plt.show() The output of the close price plot is We can see that the dates and price didn't show correct output. I have checked the type of dataframe's date. df.info() The results is I have tried some ways but it didn't work. How to solve this problem?
Don't reset the index. The index Dates is already a datetime. import pandas as pd import matplotlib.pyplot as plt %matplotlib inline import seaborn as sns sns.set() import yfinance as yf df = yf.download('AAPL', start='2001-01-01', end='2005-12-31', progress=False) # df.head() # df = df.reset_index() # <- DON'T DO THAT # df['Date'] = pd.to_datetime(df.Date, format='%Y%m%d') # <- DON'T DO THAT # df.dropna(how='any', inplace=True) # Plot the returns plt.figure(figsize=(10,6)) plt.grid(True) plt.xlabel('Dates') plt.ylabel('Prices') plt.plot(df['Close']) plt.title('Close Price', fontsize=16) plt.show() To modify the date axis, read Date tick labels from matplotlib documentation.
problem with inserting vertical lines in specific days in matplotlib date
I want to insert some vertical dashed lines in my plot. I use the following code, and I face the error " ValueError: view limit minimum -34758.04999999988 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units ". Here is a sample of my data. Date M 2013-03-13 0.727195 2013-03-14 0.727195 2013-03-15 0.727195 2013-03-16 0.727195 2013-03-17 0.727195 ... ... 2018-11-12 0.115674 2018-11-13 -0.427214 2018-11-14 -0.389715 2018-11-15 0.427149 2018-11-16 -0.416864 [2075 rows x 1 columns] and this is my code import pandas as pd from datetime import datetime, timedelta from matplotlib import pyplot as plt from matplotlib import dates as mpl_dates data=pd.read_excel('ff.xlsx') data['Date']=pd.to_datetime(data['Date'], format="%Y-%m-%d") date = data['Date'] amount = data['M'] data.set_index('Date', inplace=True, drop=True) plt.plot(date,amount, color='blue') ax = plt.axes() ax.yaxis.grid() plt.ylabel('dvv percentage') xposition = [2015-11-11, 2014-11-11] for xc in xposition: plt.axvline(x=xc, color='k', linestyle='--') plt.show()
this should work: from matplotlib import pyplot as plt import pandas as pd # example df: df = pd.DataFrame({'Date':['2013-03-13','2015-03-14','2019-03-15'], 'M':[0.727195, -0.727195, 0.669195]}) # ensure datetime: df['Date'] = pd.to_datetime(df['Date']) ax = plt.plot(df.Date, df.M, color='blue') ax = plt.axes() ax.yaxis.grid() plt.ylabel('dvv percentage') # vertical line position as datetime dtype: xposition = pd.to_datetime(['2015-11-11', '2014-11-11']) for xc in xposition: ax.axvline(x=xc, color='k', linestyle='--') plt.show()
Changing the tick frequency on the x-axis
I am trying to plot a bar chart with the date vs the price of a crypto currency from a dataframe and have 731 daily samples. When i plot the graph i get the image as seen below. Due to the amount of dates the x axis is unreadable and i would like to make it so it only labels the 1st of every month on the x-axis. This is the graph i currently have: https://imgur.com/a/QVNn4Zp I have tried using other methods i have found online both in stackoverflow and other sources such as youtube but had no success. This is the Code i have so far to plot the bar chart. df.plot(kind='bar',x='Date',y='Price in USD (at 00:00:00 UTC)',color='red') plt.show()
One option is to plot a numeric barplot with matplotlib. Matplotlib < 3.0 import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd start = pd.to_datetime("5-1-2012") idx = pd.date_range(start, periods= 365) df = pd.DataFrame({'Date': idx, 'A':np.random.random(365)}) fig, ax = plt.subplots() dates = mdates.date2num(df["Date"].values) ax.bar(dates, df["A"], width=1) loc = mdates.AutoDateLocator() ax.xaxis.set_major_locator(loc) ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(loc)) plt.show() Matplotlib >= 3.0 import numpy as np import matplotlib.pyplot as plt import pandas as pd pd.plotting.register_matplotlib_converters() start = pd.to_datetime("5-1-2012") idx = pd.date_range(start, periods= 365) df = pd.DataFrame({'Date': idx, 'A':np.random.random(365)}) fig, ax = plt.subplots() ax.bar(df["Date"], df["A"], width=1) plt.show() Further options: For other options see Pandas bar plot changes date format
How to format x axis time data from 'Month-Day Hour' (mm-dd HH) to just 'Hour' in matplotlib? Or how to place Hour data on the minor axis? [duplicate]
I have a series whose index is datetime that I wish to plot. I want to plot the values of the series on the y axis and the index of the series on the x axis. The Series looks as follows: 2014-01-01 7 2014-02-01 8 2014-03-01 9 2014-04-01 8 ... I generate a graph using plt.plot(series.index, series.values). But the graph looks like: The problem is that I would like to have only year and month (yyyy-mm or 2016 March). However, the graph contains hours, minutes and seconds. How can I remove them so that I get my desired formatting?
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # sample data N = 30 drange = pd.date_range("2014-01", periods=N, freq="MS") np.random.seed(365) # for a reproducible example of values values = {'values':np.random.randint(1,20,size=N)} df = pd.DataFrame(values, index=drange) fig, ax = plt.subplots() ax.plot(df.index, df.values) ax.set_xticks(df.index) # use formatters to specify major and minor ticks ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m")) ax.xaxis.set_minor_formatter(mdates.DateFormatter("%Y-%m")) _ = plt.xticks(rotation=90)
You can try something like this: import matplotlib.dates as mdates import matplotlib.pyplot as plt df = pd.DataFrame({'values':np.random.randint(0,1000,36)},index=pd.date_range(start='2014-01-01',end='2016-12-31',freq='M')) fig,ax1 = plt.subplots() plt.plot(df.index,df.values) monthyearFmt = mdates.DateFormatter('%Y %B') ax1.xaxis.set_major_formatter(monthyearFmt) _ = plt.xticks(rotation=90)
You should check out this native function of matplotlib: fig.autofmt_xdate() See examples on the source website Custom tick formatter
How to make bar plot with converting the month column in python?
I have a dataframe like this. The month column is in type of string. I want to make a bar plot from 201501 to 201505 with x axis is month while y axis is total_gmv. x format is like Jan,2015 Feb 2015. So how can I realize it using python? Thanks. month total_gmv 201501 NaN 201502 2.824294e+09 201503 7.742665e+09 201504 2.024132e+10 201505 6.705012e+10
import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame( {'month': ['201501', '201502', '201503', '201504', '201505'], 'total_gmv': [np.nan, 2.824294e+09, 7.742665e+09, 2.024132e+10, 6.705012e+10]}) df['month'] = pd.to_datetime(df['month'], format='%Y%m').dt.month df = df.set_index('month') print df df.plot(kind='bar') plt.show() Result: total_gmv month 1 NaN 2 2.824294e+09 3 7.742665e+09 4 2.024132e+10 5 6.705012e+10
You should be able to force month to be a timestamp and then set it as an index and plot it. df['month'] = pd.to_datetime(df.month) ax = df.set_index('month').plot(kind='bar') And you might have to change the date format. import matplotlib.dates as mdates ax.xaxis.set_major_formatter= mdates.DateFormatter('%b, %Y') Check here for more
Previous replies have some clues but its does not show exhaustive answer. You have to set custom xtick labels and rotate it like here: import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame( {'month': ['201501', '201502', '201503', '201504', '201505'], 'total_gmv': [np.nan, 2.824294e+09, 7.742665e+09, 2.024132e+10, 6.705012e+10]}) df['month'] = pd.to_datetime(df['month'], format='%Y%m', errors='ignore') ax = df.plot(kind='bar') ax.set_xticklabels(df['month'].dt.strftime('%b, %Y')) plt.xticks(rotation=0) plt.show()
You should use matplotlib.pyplot and calendar module. import matplotlib.pyplot as plt import calendar #change the numeric representation to texts (201501 -> Jan,2015) df['month_name'] = [','.join([calendar.month_name[int(date[-1:-3]),date[-3:]] for date in df['month'] #change the type of df['month'] to int so plt can read it df['month'].apply(int) x = df['month'] y = df['total_gmv'] plt.bar(x, y, align = 'center') #i'm not sure if you have to change the Series to a list; do whatever works plt.xticks =(x, df['month_name']) plt.show()