0

I want to do the bar plot using pandas but the xticks are incorrect for date type (there are integers starting from 0). Here is my code:

import matplotlib.pyplot as plt
import datetime as dt
import pandas as pd

y = [dt.date(2021, 12, 12), dt.date(2022, 6, 12), dt.date(2023, 12, 12)]
df = pd.DataFrame()
df["Date"] = y
df["V1"] = [0, 0.2, 0.8]
df["V2"] = [0.4, 0.3, 0.3]
# df.set_index("Date", inplace = True)
ax = df.plot.bar(x = "Date", stacked = True)
print(ax.get_xticks())

fig, ax = plt.subplots()
ax.plot(df["Date"], df["V1"])
print(ax.get_xticks())

How can I fix that?

Berkos
  • 13
  • 3
  • For me, matplotlib renders the tick labels in both graphs with the date, so it seems to be working as intended. It seems that pandas does this by setting the tick label rather than the tick location. Is there a specific reason you need the ticks to not be [0, 1, 2]? – Nick ODell May 03 '23 at 20:52
  • yes I want to twin `x` axis (dates) and add line plot to the abr plot like this : `ax2 = ax.twinx() ax2.plot(y, [1,2,3])` With not correctly formatter xticks it doesnt work properly – Berkos May 03 '23 at 20:55

0 Answers0