Python matplotlib time series plot -- show years on x axis - python

The Python code
import matplotlib.pyplot as plt
...
xx["SPY"].plot()
plt.show()
where xx is a data frame with a date index produces a time series plot where the dates 11/30/2010, 9/15/2011, 7/2/2012 etc. are shown as labels on the x axis. I would like to labels corresponding to years, so that only "2010", "2011", "2012" etc. are shown on the x axis. How can this be done?

Related

Seaborn stack barplot and lineplot on a single plot with datetime x axis [duplicate]

This question already has answers here:
Barplot and line plot in seaborn/matplotlib
(1 answer)
How to line plot timeseries data on a bar plot
(1 answer)
pandas bar plot combined with line plot shows the time axis beginning at 1970
(2 answers)
Closed 4 months ago.
I have barplot and lineplots that share the same x axis that I want to plot together. Here's the picture:
I want the graph plot to keep the "average_daily_price" as y axis and disregard "num_sales" as y axis. Here's the result I want to achieve:
I've tried the following
fig, ax1 = plt.subplots()
sns.lineplot(filtered_df, x='date', y='average_daily_price', ax=ax1)
sns.barplot(filtered_df, x="date", y="num_sales", alpha=0.5, ax=ax1)
But it gives weird result. I've also tried twinx() but couldn't make it work, besides it creates second y axis which I don't want.
Edit: running rafael's code results in this plot:
I'd like to add that date is in a datetime64[ns] format.
Edit 2: This post has been closed for duplicate. I've already seen the posts in duplicate list and tried the solutions listed, but they do not apply to my case, I don't know why, that's what I'm trying to figure out by opening new question. I'm guessing it has to do with my x variable being a datetime object.
The seaborn "barplot" is dedicated to plotting categorical variables. As such, it understands that each date is an unique value and plots the corresponding values sequentially.
This breaks the behavior of the dates in the x-axis.
A workaround for this is to use matplotlibs ax.bar directly:
# imports
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
# generate dummy data
rng = np.random.default_rng()
size=100
vals = rng.normal(loc=0.02,size=size).cumsum() + 50
drange = pd.date_range("2014-01", periods=size, freq="D")
num_sales = rng.binomial(size=size,n=50,p=0.4)
# store data in a pandas DF
df = pd.DataFrame({'date': drange,
'average_daily_price': vals,
'num_sales': num_sales})
# setup axes
fig, ax1 = plt.subplots(figsize=(12,3))
# double y-axis is necessary due to the difference in the range of both variables
ax2 = ax1.twinx()
# plot the number of sales as a series of vertical bars
ax2.bar(df['date'], df['num_sales'], color='grey', alpha=0.5, label='Number of sales')
# plot the price as a time-series line plot
sns.lineplot(data=df, x='date', y='average_daily_price', ax=ax1)
# format the x-axis ticks as dates in weekly intervals
# the format is datetime64[ns]
ax1.xaxis.set_major_locator(mpl.dates.WeekdayLocator(interval=1, byweekday=1)) #weekly
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y-%m-%d'))
# rotate the x-axis tick labels for readability
ax1.tick_params(axis='x', rotation=50)
and the output is

Plotting a pandas dataframe using column names as x axis

I have the following Pandas Dataframe (linked above) and I'd like to plot a graph with the values 1.0 - 39.0 on the x axis and the y axis would be the dataframe values in the column of these (-0.004640 etc). The rows are other lines I'd like to plot, so at the end there will be a lot of lines.
I've tried to transpose my plot but that doesn't seem to work.
How could I go about doing this?
You could try to use matplotlib:
import matplotlib.pyplot as plt
%matplotlib inline
x=[1.0, 39.0]
plt.plot(x, df[1.0])
plt.plot(x, df[2.0})
...

Organizing Plots in Seaborn Pairplot

I've got a pandas dataframe with a bunch of values in and I want to plot each axis against each axis to get plots of every column against one another. Furthermore, I'm having an issue of the values of my y axis being so condensed that's it's unreadable. I've tried changing the height but have no clue how to "clean up" this axis.
Here is my plotting code:
import seaborn as sns
grid = sns.pairplot(df_merge, dropna = True, height=1.5)
Then here is the graph that has been plotted.

How to make X-axis more clear if it's values are a bit lengthy?

I have a string array which consists of minutes and seconds like 10:03, 10:13,..... at an interval of ten seconds. There are 360 values in this array and I want to plot it on x-axis but the graph looks like this:
Time array on X-axis
It is showing all the points on X-axis which is why there is a huge black bar. How do I make the X-axis more clear so that it only shows a few points on the plot?
this code may be helpful:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
y = x**2
plt.plot(x,y)
plt.xticks(range(0,100,10),["number"+str(i) for i in x], rotation=90)
plt.show()

Matplotlib Interactive subplots with integers and dates on X axes

I am trying to create two interactive plots where the first plot is simply a plot of x and y and the second plot is a subplot which plots dates (fulldate) on its x axis, which correspond to the integer values of x (x axis values) from the first plot.
This code almost does what I want. The only problem is that the dates are not linked to the integers, so when I use the zoom function on the graph, it zooms into the first plot and the subplot is linked and zooms also, but the dates stay stationary and therefore are completely inaccurate.
note that this is just a simplified version of my program. i will be rearranging the dates on the bottom display to got on the bottom.
The integers and the dates must be linked because in my actual program i will be using integers to keep count of the days in the time series.
import matplotlib.pyplot as plt
import seaborn as sns
x=[1,5,7,4,6]
y=[1,3,8,4,6]
fulldate=['01/01/2018','02/01/2018','03/01/2018','04/01/2018','05/01/2018']
with sns.axes_style("darkgrid"):
ax1=plt.subplot2grid((6,1),(0,0),rowspan=3,colspan=1)
ax2=plt.subplot2grid((6,1),(4,0),rowspan=1,colspan=1,sharex=ax1)
ax2v = ax2.twiny()
ax1.plot(x,y)
ax2v.fill_between(fulldate,'Dates')
for label in ax2v.xaxis.get_ticklabels():
label.set_rotation(60)

Categories