Issue
How do I remove the time from x axis with Dataframe.plot
?
This is my code:
data.reset_index().plot(x='Date', y='Open', kind = 'bar', colormap='gist_rainbow')
Solution
Edit: OP precised that the question was about formatting and not removing dates.
Use matplotlib directly instead of pandas.
Reference: matplotlib.dates.DateFormatter
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.dates import DateFormatter
df = pd.DataFrame({
"Date": pd.date_range("2020-01-01", freq="MS", periods=6),
"A": [0, 1, 2, 3, 4, 5],
"B": [0, 1, 4, 9, 16, 25]
})
fig, ax = plt.subplots()
plt.bar("Date", "B", data=df, axes=ax, width=5) # Set width for larger bars
dfmt = DateFormatter("%Y-%m-%d") # proper formatting Year-month-day
ax.xaxis.set_major_formatter(dfmt)
ax.set_xlabel("Date") # Naming the x-axis
plt.xticks(rotation=45) # Optionam but helps when dates are long
plt.show()
Answered By - Nathan Furnal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.