0

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://i.stack.imgur.com/sBeZl.jpg

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()
vimuth
  • 5,064
  • 33
  • 79
  • 116

1 Answers1

0

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712