diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index b15c5271ae321..3e186b7e4aaee 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -561,12 +561,14 @@ def _get_xticks(self, convert_period=False): if convert_period and isinstance(index, PeriodIndex): self.data = self.data.reindex(index=index.sort_values()) x = self.data.index.to_timestamp()._mpl_repr() - elif index.is_numeric(): + elif (index.is_numeric() or + index.inferred_type in ['string', 'unicode']): """ Matplotlib supports numeric values or datetime objects as xaxis values. Taking LBYL approach here, by the time matplotlib raises exception when using non numeric/datetime values for xaxis, several actions are already taken by plt. + Matplotlib also supports strings as xaxis values. """ x = index._mpl_repr() elif is_datetype: diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 3d25b0b51e052..6dcd342b94a47 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -139,6 +139,28 @@ def test_plot(self): result = ax.get_axes() # deprecated assert result is axes[0] + # GH 18726 + def test_two_plots_with_x_str(self): + x1, y1 = ['a', 'b'], [1, 2] + x2, y2 = ['b', 'a'], [4, 3] + df1 = pd.DataFrame(y1, index=x1) + df2 = pd.DataFrame(y2, index=x2) + ax = None + ax = df1.plot(ax=ax) + ax = df2.plot(ax=ax) + + line1, line2 = ax.lines + + # xdata should not be touched (Earlier it was [0, 1]) + tm.assert_numpy_array_equal(line1.get_xdata(), np.array(x1), + check_dtype=False) + tm.assert_numpy_array_equal(line1.get_ydata(), np.array(y1), + check_dtype=False) + tm.assert_numpy_array_equal(line2.get_xdata(), np.array(x2), + check_dtype=False) + tm.assert_numpy_array_equal(line2.get_ydata(), np.array(y2), + check_dtype=False) + # GH 15516 def test_mpl2_color_cycle_str(self): # test CN mpl 2.0 color cycle