Reputation: 515
I have the following pandas dataframe
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import time
data = {'date': ['09:50:00', '09:55:00', '10:00:00', '10:05:00', '10:10:00', '10:15:00', '10:20:00', '10:25:00', '10:30:00', '10:35:00'],
'bidopen': [1.33872, 1.33824, 1.33768, 1.33732, 1.33786, 1.33798, 1.33752, 1.33749, 1.33718, 1.3374],
'bidclose': [1.33824, 1.33768, 1.33732, 1.33786, 1.33798, 1.33752, 1.33749, 1.33718, 1.3374, 1.33766],
'bidhigh': [1.33889, 1.33832, 1.33774, 1.33786, 1.33814, 1.33815, 1.33776, 1.33781, 1.33765, 1.33769],
'bidlow': [1.33822, 1.33766, 1.33716, 1.33725, 1.3378, 1.33747, 1.33742, 1.33712, 1.33714, 1.33729],
'askopen': [1.33891, 1.33842, 1.33787, 1.33751, 1.33803, 1.33816, 1.33768, 1.3377, 1.33735, 1.3376],
'askclose': [1.33842, 1.33787, 1.33751, 1.33803, 1.33816, 1.33768, 1.3377, 1.33735, 1.3376, 1.33785],
'askhigh': [1.33908, 1.3385, 1.33793, 1.33804, 1.33832, 1.33834, 1.33795, 1.338, 1.33782, 1.33787],
'asklow': [1.33838, 1.33784, 1.33734, 1.33743, 1.33799, 1.33765, 1.33762, 1.33728, 1.33733, 1.33749],
'tickqty': [875, 818, 886, 600, 704, 837, 947, 1010, 1178, 567]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.time
print(df.head(10).to_string())
plt.plot(df['date'], df['bidhigh'])
plt.show()
I am unable to plot the time column on the x-axis and the bidghigh
column on the y-axis. I get this error:
Traceback (most recent call last):
File "<ipython-input-55-3572052619ce>", line 1, in <module>
plt.plot(x, y)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2825, in plot
**({"data": data} if data is not None else {}), **kwargs)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 1745, in plot
self.add_line(line)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 1969, in add_line
self._update_line_limits(line)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 1991, in _update_line_limits
path = line.get_path()
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/lines.py", line 1011, in get_path
self.recache()
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/lines.py", line 653, in recache
x = _to_unmasked_float_array(xconv).ravel()
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 1289, in _to_unmasked_float_array
return np.asarray(x, float)
File "/home/mahmoud/anaconda3/envs/fxenv/lib/python3.7/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'datetime.time'
My question is: how do I plot the date column on the x-axis and the bidhigh
column on the y-axis?
Upvotes: 1
Views: 12022
Reputation: 3108
Your issue is the df['date'] = df['date'].dt.time
line.
Removing that line, and leaving the column as Timestamp objects results in a no errors. You can always format the display on the axis with:
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%H:%M:%S')
# Check https://strftime.org/ for formatters
ax.xaxis.set_major_formatter(myFmt)
Take a look at this post for more details.
As an example, I plot bidopen
with respect to timestamp:
I have formatted the axis as Hour:Min:Seconds and rotated the lables by 45 degrees with plt.xticks(rotation=45)
for readability.
Code Listing (not best practice):
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv("/tmp/SO_63363129.csv")
df['timestamp'] = pd.to_datetime(df['date'])
fig, ax = plt.subplots()
ax.plot(df['timestamp'], df['bidopen'))
ax.format_xdata = mdates.DateFormatter('%H:%M:%S')
plt.xticks(rotation=45)
plt.show()
Upvotes: 1