Repository: pandas-dev/pandas

Issue:
df.plot with two plots in the same axes and 2nd one is line plot makes different chart between 0.23.4 and 0.25.0
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('iris.csv')
dfg = df.groupby('species').mean()
dfg['swratio'] = dfg.sepal_width / dfg.sepal_length

fig, ax = plt.subplots(figsize=(10, 6))
dfg[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']].plot.bar(ax=ax, rot=0, stacked=False)
dfg[['swratio']].plot.line(label='Sepal Width Ratio', 
                 ax=ax, secondary_y=True, mark_right=False, 
                 rot=0, marker='o', markersize=3, color='c')
ax.grid(True)
pngfile = 'Output.png'
fig = plt.gcf()
fig.savefig(pngfile, bbox_inches = 'tight', dpi=100)
```
#### Problem description

dataframe.plot with secondary_y=True makes different chart between 0.23.4 and 0.25.0 as attached.
![GeneratedFromPahdas0 23 4](https://user-images.githubusercontent.com/26564474/62275057-13a47480-b47c-11e9-8020-dc137be70aa6.png)
![GeneratedFromPahdas0 25 0](https://user-images.githubusercontent.com/26564474/62275058-143d0b00-b47c-11e9-8ef4-8152bfda8f71.png)

**Note**: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

**Note**: Many problems can be resolved by simply upgrading `pandas` to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if `master` addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on `master` here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

#### Expected Output

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

</details>



Discussion:

Resolving patch:
diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py
--- a/pandas/plotting/_matplotlib/core.py
+++ b/pandas/plotting/_matplotlib/core.py
@@ -33,8 +33,6 @@
 from pandas.plotting._matplotlib.style import _get_standard_colors
 from pandas.plotting._matplotlib.tools import (
     _flatten,
-    _get_all_lines,
-    _get_xlim,
     _handle_shared_axes,
     _subplots,
     format_date_labels,
@@ -1101,9 +1099,8 @@ def _make_plot(self):
             )
             self._add_legend_handle(newlines[0], label, index=i)
 
-            lines = _get_all_lines(ax)
-            left, right = _get_xlim(lines)
-            ax.set_xlim(left, right)
+            # GH27686 set_xlim will truncate xaxis to fixed space
+            ax.relim()
 
     @classmethod
     def _plot(cls, ax, x, y, style=None, column_num=None, stacking_id=None, **kwds):
diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py
--- a/pandas/plotting/_matplotlib/tools.py
+++ b/pandas/plotting/_matplotlib/tools.py
@@ -343,27 +343,6 @@ def _flatten(axes):
     return np.array(axes)
 
 
-def _get_all_lines(ax):
-    lines = ax.get_lines()
-
-    if hasattr(ax, "right_ax"):
-        lines += ax.right_ax.get_lines()
-
-    if hasattr(ax, "left_ax"):
-        lines += ax.left_ax.get_lines()
-
-    return lines
-
-
-def _get_xlim(lines):
-    left, right = np.inf, -np.inf
-    for l in lines:
-        x = l.get_xdata(orig=False)
-        left = min(np.nanmin(x), left)
-        right = max(np.nanmax(x), right)
-    return left, right
-
-
 def _set_ticks_props(axes, xlabelsize=None, xrot=None, ylabelsize=None, yrot=None):
     import matplotlib.pyplot as plt
 


Test patch:
diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py
--- a/pandas/tests/plotting/test_datetimelike.py
+++ b/pandas/tests/plotting/test_datetimelike.py
@@ -419,6 +419,8 @@ def test_get_finder(self):
         assert conv.get_finder("A") == conv._annual_finder
         assert conv.get_finder("W") == conv._daily_finder
 
+    # TODO: The finder should be retested due to wrong xlim values on x-axis
+    @pytest.mark.xfail(reason="TODO: check details in GH28021")
     @pytest.mark.slow
     def test_finder_daily(self):
         day_lst = [10, 40, 252, 400, 950, 2750, 10000]
@@ -442,6 +444,8 @@ def test_finder_daily(self):
         assert rs1 == xpl1
         assert rs2 == xpl2
 
+    # TODO: The finder should be retested due to wrong xlim values on x-axis
+    @pytest.mark.xfail(reason="TODO: check details in GH28021")
     @pytest.mark.slow
     def test_finder_quarterly(self):
         yrs = [3.5, 11]
@@ -465,6 +469,8 @@ def test_finder_quarterly(self):
         assert rs1 == xpl1
         assert rs2 == xpl2
 
+    # TODO: The finder should be retested due to wrong xlim values on x-axis
+    @pytest.mark.xfail(reason="TODO: check details in GH28021")
     @pytest.mark.slow
     def test_finder_monthly(self):
         yrs = [1.15, 2.5, 4, 11]
@@ -498,6 +504,8 @@ def test_finder_monthly_long(self):
         xp = Period("1989Q1", "M").ordinal
         assert rs == xp
 
+    # TODO: The finder should be retested due to wrong xlim values on x-axis
+    @pytest.mark.xfail(reason="TODO: check details in GH28021")
     @pytest.mark.slow
     def test_finder_annual(self):
         xp = [1987, 1988, 1990, 1990, 1995, 2020, 2070, 2170]
@@ -522,7 +530,7 @@ def test_finder_minutely(self):
         _, ax = self.plt.subplots()
         ser.plot(ax=ax)
         xaxis = ax.get_xaxis()
-        rs = xaxis.get_majorticklocs()[0]
+        rs = xaxis.get_majorticklocs()[1]
         xp = Period("1/1/1999", freq="Min").ordinal
 
         assert rs == xp
@@ -534,7 +542,7 @@ def test_finder_hourly(self):
         _, ax = self.plt.subplots()
         ser.plot(ax=ax)
         xaxis = ax.get_xaxis()
-        rs = xaxis.get_majorticklocs()[0]
+        rs = xaxis.get_majorticklocs()[1]
         xp = Period("1/1/1999", freq="H").ordinal
 
         assert rs == xp
@@ -1410,7 +1418,9 @@ def test_plot_outofbounds_datetime(self):
 
     def test_format_timedelta_ticks_narrow(self):
 
-        expected_labels = ["00:00:00.0000000{:0>2d}".format(i) for i in range(10)]
+        expected_labels = [
+            "00:00:00.0000000{:0>2d}".format(i) for i in np.arange(0, 10, 2)
+        ]
 
         rng = timedelta_range("0", periods=10, freq="ns")
         df = DataFrame(np.random.randn(len(rng), 3), rng)
@@ -1420,8 +1430,8 @@ def test_format_timedelta_ticks_narrow(self):
         labels = ax.get_xticklabels()
 
         result_labels = [x.get_text() for x in labels]
-        assert len(result_labels) == len(expected_labels)
-        assert result_labels == expected_labels
+        assert (len(result_labels) - 2) == len(expected_labels)
+        assert result_labels[1:-1] == expected_labels
 
     def test_format_timedelta_ticks_wide(self):
         expected_labels = [
@@ -1444,8 +1454,8 @@ def test_format_timedelta_ticks_wide(self):
         labels = ax.get_xticklabels()
 
         result_labels = [x.get_text() for x in labels]
-        assert len(result_labels) == len(expected_labels)
-        assert result_labels == expected_labels
+        assert (len(result_labels) - 2) == len(expected_labels)
+        assert result_labels[1:-1] == expected_labels
 
     def test_timedelta_plot(self):
         # test issue #8711
diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py
--- a/pandas/tests/plotting/test_frame.py
+++ b/pandas/tests/plotting/test_frame.py
@@ -3177,6 +3177,58 @@ def test_x_multiindex_values_ticks(self):
         assert labels_position["(2013, 1)"] == 2.0
         assert labels_position["(2013, 2)"] == 3.0
 
+    @pytest.mark.parametrize("kind", ["line", "area"])
+    def test_xlim_plot_line(self, kind):
+        # test if xlim is set correctly in plot.line and plot.area
+        # GH 27686
+        df = pd.DataFrame([2, 4], index=[1, 2])
+        ax = df.plot(kind=kind)
+        xlims = ax.get_xlim()
+        assert xlims[0] < 1
+        assert xlims[1] > 2
+
+    def test_xlim_plot_line_correctly_in_mixed_plot_type(self):
+        # test if xlim is set correctly when ax contains multiple different kinds
+        # of plots, GH 27686
+        fig, ax = self.plt.subplots()
+
+        indexes = ["k1", "k2", "k3", "k4"]
+        df = pd.DataFrame(
+            {
+                "s1": [1000, 2000, 1500, 2000],
+                "s2": [900, 1400, 2000, 3000],
+                "s3": [1500, 1500, 1600, 1200],
+                "secondary_y": [1, 3, 4, 3],
+            },
+            index=indexes,
+        )
+        df[["s1", "s2", "s3"]].plot.bar(ax=ax, stacked=False)
+        df[["secondary_y"]].plot(ax=ax, secondary_y=True)
+
+        xlims = ax.get_xlim()
+        assert xlims[0] < 0
+        assert xlims[1] > 3
+
+        # make sure axis labels are plotted correctly as well
+        xticklabels = [t.get_text() for t in ax.get_xticklabels()]
+        assert xticklabels == indexes
+
+    def test_subplots_sharex_false(self):
+        # test when sharex is set to False, two plots should have different
+        # labels, GH 25160
+        df = pd.DataFrame(np.random.rand(10, 2))
+        df.iloc[5:, 1] = np.nan
+        df.iloc[:5, 0] = np.nan
+
+        figs, axs = self.plt.subplots(2, 1)
+        df.plot.line(ax=axs, subplots=True, sharex=False)
+
+        expected_ax1 = np.arange(4.5, 10, 0.5)
+        expected_ax2 = np.arange(-0.5, 5, 0.5)
+
+        tm.assert_numpy_array_equal(axs[0].get_xticks(), expected_ax1)
+        tm.assert_numpy_array_equal(axs[1].get_xticks(), expected_ax2)
+
 
 def _generate_4_axes_via_gridspec():
     import matplotlib.pyplot as plt
diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py
--- a/pandas/tests/plotting/test_series.py
+++ b/pandas/tests/plotting/test_series.py
@@ -897,3 +897,15 @@ def test_plot_accessor_updates_on_inplace(self):
         _, ax = self.plt.subplots()
         after = ax.xaxis.get_ticklocs()
         tm.assert_numpy_array_equal(before, after)
+
+    @pytest.mark.parametrize("kind", ["line", "area"])
+    def test_plot_xlim_for_series(self, kind):
+        # test if xlim is also correctly plotted in Series for line and area
+        # GH 27686
+        s = Series([2, 3])
+        _, ax = self.plt.subplots()
+        s.plot(kind=kind, ax=ax)
+        xlims = ax.get_xlim()
+
+        assert xlims[0] < 0
+        assert xlims[1] > 1