I am trying to format the date in matplotlib but rather then show correct values, it always retrns 00:00
, here is the code
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def create_chart():
stored_prices = [0.003197, 0.003197, 0.003197, 0.003197, 0.003197]
prices_time = ['Thursday:09:00', 'Thursday:09:05', 'Thursday:09:10', 'Thursday:09:15', 'Thursday:09:20']
fig, ax = plt.subplots()
plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)
plt.plot(prices_time, stored_prices)
plt.grid()
plt.subplots_adjust(left=0.03, right=0.86)
date_form = mdates.DateFormatter("%H:%M")
ax.xaxis.set_major_formatter(date_form)
print(stored_prices, prices_time)
plt.show() #Preview of chart
create_chart()
CodePudding user response:
You are plotting x axis as str
, but you firstly need to convert it to datetime
.
You can store converted values in an other variable, time_only
and then use it to draw the plot:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
def create_chart():
stored_prices = [0.003197, 0.003197, 0.003197, 0.003197, 0.003197]
prices_time = ['Thursday:09:00', 'Thursday:09:05', 'Thursday:09:10', 'Thursday:09:15', 'Thursday:09:20']
time_only = [datetime.strptime(hour, '%A:%H:%M') for hour in prices_time]
fig, ax = plt.subplots()
plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)
plt.plot(time_only, stored_prices)
plt.grid()
plt.subplots_adjust(left=0.03, right=0.86)
date_form = mdates.DateFormatter("%H:%M")
ax.xaxis.set_major_formatter(date_form)
print(stored_prices, prices_time)
plt.show() #Preview of chart
create_chart()