Skip to content

twinx plot with datetime in xaxis fails in 3.1.3 #16405

Description

@icfly2

Bug report

Bug summary

in 3.1.3 ax.twinx() with fails to plot the second plot if using datetime values for x axis.

Code for reproduction

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np

df = pd.DataFrame(index=pd.date_range(start='2020-01-26 22:00:00+00:00', end='2020-02-03 14:00:00+00:00', freq='H'),
                 data=np.random.rand(185, 5), columns = ['code_200','code_201','code_500','mean_200','mean_201'] )
fig, ax = plt.subplots( figsize=(15,6))
# plt.figure()
dfd = df.resample('D')[['code_200','code_201','code_500']].sum().tail(10)
# plt.bar(dfd)
plt.bar(dfd.index, dfd.code_200, color='#008c6e', label='sum of 200s')
plt.bar(dfd.index, dfd.code_201, bottom=dfd.code_200, color='k', label='sum of 201s')
plt.bar(dfd.index, dfd.code_500, bottom=dfd.code_200+dfd.code_201, color='#872d32',label='sum of 500s')

ax.yaxis.grid('both',color='k', linestyle='-', alpha=0.2)
ax.set_ylabel('# responses')
dfm = df.resample('D')[['mean_200', 'mean_201']].mean().tail(10)
ax2 = ax.twinx() 
ax2.plot(dfm.index, dfm.mean_200.values, label='avg for 200s', color='#808080')
ax2.plot(dfm.index, dfm.mean_201.values, label='avg for 201s', color='#872d32')
ax.legend(loc='upper left')
ax2.legend(loc='lower right')
ax2.set_ylabel('average response times [ms]')
ax.xaxis.set_major_formatter( mdates.DateFormatter('%a, %d-%b'))
plt.show()

Actual outcome

ax2.plot(dfm.index, dfm.mean_200.values, label='avg for 200s', color='#808080')
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 1667, in plot
    self.add_line(line)
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 1902, in add_line
    self._update_line_limits(line)
  File "/usr/local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 1924, in _update_line_limits
    path = line.get_path()
  File "/usr/local/lib/python3.7/site-packages/matplotlib/lines.py", line 1027, in get_path
    self.recache()
  File "/usr/local/lib/python3.7/site-packages/matplotlib/lines.py", line 670, in recache
    x = _to_unmasked_float_array(xconv).ravel()
  File "/usr/local/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 1390, in _to_unmasked_float_array
    return np.asarray(x, float)
  File "/usr/local/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 'Timestamp'

Expected outcome

image

The image was generated with matplotlib version 3.1.0 it fails in 3.1.3

Matplotlib version

  • Operating system: Works with 3.1.0 on Windows 10 and Linux (python docker)
  • Matplotlib version: 3.1.0 / 3.1.3
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 3.7
  • Jupyter version (if applicable): nope
  • Other libraries: pandas = 1.0.0 and 0.24.1 both work / fail

All installed from pip.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions