Issue
I want to create a bar plot where the x-axis is the datetime
column from my data-frame.
I converted the datetime
in pandas
column to time only and I am getting an error when I try to use the matplotlib.pyplot.barplot
function to plot the data:
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('data.csv')
dataset['date'] = pd.to_datetime(dataset['date']).dt.time
plt.bar(dataset['date'], dataset['tickqty'])
plt.show()
I get the following error:
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'float'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-2-7ddd22154cf4>", line 7, in <module>
plt.bar(dataset['date'], dataset['tickqty'])
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/sitepackages/matplotlib/pyplot.py", line 2472, in bar
**({"data": data} if data is not None else {}), **kwargs)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/__init__.py", line 1431, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 2464, in bar
f'are incompatible') from e
TypeError: the dtypes of parameters x (object) and width (float64) are incompatible
This is the data from the csv file:
How can I plot the time column in matplotlib.pyplot.barplot
?
Solution
Solution with matplotlib bar plot
pd.to_datetime(dataset['date']).dt.time
converts the dates to strings (dtype object
) which is causing the TypeError
. For plt.bar
to work, you need to use numerical data. You can use the full dates stored as pandas Timestamp
objects that you get when using pd.to_datetime(dataset['date'])
and matplotlib will automatically process them as matplotlib date units (number of days since 1970-01-01 UTC).
Then you need to format the x-axis tick labels so that only the times are shown. This can be done by using the matplotlib.dates DateFormatter
with the appropriate format codes. Here is an example using the data you shared in the image:
import pandas as pd # v 1.1.3
import matplotlib.pyplot as plt # v 3.3.2
import matplotlib.dates as mdates
dataset = pd.read_csv('data.csv')
dataset['date'] = pd.to_datetime(dataset['date'])
plt.bar(dataset['date'], dataset['tickqty'], width=0.002)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.show()
Note that the bar width
argument is using the x variable unit (i.e. matplotlib date units) which is time in days. So 0.002
corresponds to about 2 minutes and 53 seconds.
Answered By - Patrick FitzGerald
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.