Skip to content

bugfix for plot for string x values #18726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down