<h3 dir="auto">Code Sample from Stamford</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[http://stamfordresearch.com/linear-regression-using-pandas-python/](url) (original code)

[](url)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame ({
'length' : [94,74,147,58,86,94,63,86,69,72,128,85,82,86,88,72,74,61,90,89,68,76,114,90,78],
'weight' : [130,51,640,28,80,110,33,90,36,38,366,84,80,83,70,61,54,44,106,84,39,42,197,102,57]
})
 
# create another data frame of log values
data_log = np.log(data)


# ========================
# Model for Original Data
# ========================
 
lm_original = np.polyfit(data.length, data.weight, 1)
polynomial = np.poly1d(lm_original)
y =polynomial(data.length)

lm_original_plot=pd.DataFrame({
    'length': data.length,
    'weight': y
        })

# ========================
# Model for Log Data
# ========================
 
# Get the linear models
lm_log = np.polyfit(data_log.length, data_log.weight, 1)
 
# calculate the y values based on the co-efficients from the model
r_x, r_y = zip(*((i, i*lm_log[0] + lm_log[1]) for i in data_log.length))
 
# Put in to a data frame, to keep is all nice
lm_log_plot = pd.DataFrame({
'length' : r_x,
'weight' : r_y
})


# ========================
# Plot the data
# ========================
fig, axes = plt.subplots(nrows=1, ncols=2)
 
# Plot the original data and model
data.plot(kind='scatter', color='Blue', x='length', y='weight', ax=axes[0],title='Original Values')
lm_original_plot.plot(kind='line', color='Red', x='length', y='weight', ax=axes[0])
 
# Plot the log transformed data and model
data_log.plot(kind='scatter', color='Blue', x='length', y='weight', ax=axes[1], title='Log Values')
lm_log_plot.plot(kind='line', color='Red', x='length', y='weight', ax=axes[1])

plt.show()"><pre class="notranslate"><code class="notranslate">[http://stamfordresearch.com/linear-regression-using-pandas-python/](url) (original code)

[](url)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame ({
'length' : [94,74,147,58,86,94,63,86,69,72,128,85,82,86,88,72,74,61,90,89,68,76,114,90,78],
'weight' : [130,51,640,28,80,110,33,90,36,38,366,84,80,83,70,61,54,44,106,84,39,42,197,102,57]
})
 
# create another data frame of log values
data_log = np.log(data)


# ========================
# Model for Original Data
# ========================
 
lm_original = np.polyfit(data.length, data.weight, 1)
polynomial = np.poly1d(lm_original)
y =polynomial(data.length)

lm_original_plot=pd.DataFrame({
    'length': data.length,
    'weight': y
        })

# ========================
# Model for Log Data
# ========================
 
# Get the linear models
lm_log = np.polyfit(data_log.length, data_log.weight, 1)
 
# calculate the y values based on the co-efficients from the model
r_x, r_y = zip(*((i, i*lm_log[0] + lm_log[1]) for i in data_log.length))
 
# Put in to a data frame, to keep is all nice
lm_log_plot = pd.DataFrame({
'length' : r_x,
'weight' : r_y
})


# ========================
# Plot the data
# ========================
fig, axes = plt.subplots(nrows=1, ncols=2)
 
# Plot the original data and model
data.plot(kind='scatter', color='Blue', x='length', y='weight', ax=axes[0],title='Original Values')
lm_original_plot.plot(kind='line', color='Red', x='length', y='weight', ax=axes[0])
 
# Plot the log transformed data and model
data_log.plot(kind='scatter', color='Blue', x='length', y='weight', ax=axes[1], title='Log Values')
lm_log_plot.plot(kind='line', color='Red', x='length', y='weight', ax=axes[1])

plt.show()
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Pandas doesn't plot the expected graph as the website mention in it.<br>
the bug is at this line:<br>
data.plot(kind='scatter', color='Blue', x='length', y='weight', ax=axes[0],title='Original Values')<br>
lm_original_plot.plot(kind='line', color='Red', x='length', y='weight', ax=axes[0])<br>
The x-axis data is being flipped when pandas try to plot it.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/29069095/26596610/ed284bf6-45a1-11e7-8cbe-a45721f2ace3.png"><img src="https://cloud.githubusercontent.com/assets/29069095/26596610/ed284bf6-45a1-11e7-8cbe-a45721f2ace3.png" alt="image" style="max-width: 100%;"></a></p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/29069095/26596592/e070bec0-45a1-11e7-80a9-3f8486c80e3d.png"><img src="https://cloud.githubusercontent.com/assets/29069095/26596592/e070bec0-45a1-11e7-80a9-3f8486c80e3d.png" alt="image" style="max-width: 100%;"></a></p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None
<p dir="auto">pandas: 0.20.1<br>
pytest: 3.0.5<br>
pip: 9.0.1<br>
setuptools: 35.0.2<br>
Cython: 0.25.2<br>
numpy: 1.12.1<br>
scipy: 0.19.0<br>
xarray: None<br>
IPython: 6.0.0<br>
sphinx: 1.6.2<br>
patsy: 0.4.1<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.2.2<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.0.2<br>
openpyxl: 2.4.1<br>
xlrd: 1.0.0<br>
xlwt: 1.2.0<br>
xlsxwriter: 0.9.6<br>
lxml: 3.7.2<br>
bs4: 4.5.3<br>
html5lib: 0.999999999<br>
sqlalchemy: 1.1.5<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">X axis was inverted automatically and unexpectedly when plotting a series of data against another series of data using pandas.<br>
My example code blow creates three plots, only some, not all, of which shows inverted x axis. I think this behavior is very confusing for users even if there was some rationale behind it. IMHO, automatic inversion of x axis is unnecessary because a user can use invert_xaxis() in case one wants to invert it. On <a href="http://stackoverflow.com/questions/29908874/x-axis-inverted-unexpectedly-by-pandas-plot" rel="nofollow">stackoverflow</a>, a workaround was suggested, but no direct solution.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.randn(10, 3), columns=[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])
df3 = df2*1.1

df3.rename(columns={&quot;a&quot;: &quot;a*1.1&quot;, &quot;b&quot;: &quot;b*1.1&quot;, &quot;c&quot;: &quot;c*1.1&quot;}, inplace=True)
df23 = df2.join(df3)

fig, ax_list = plt.subplots(1,3)

ax=ax_list[0]
df23[[&quot;a&quot;, &quot;a*1.1&quot;]].plot(ax=ax, x=&quot;a&quot;)
ax.axis('equal')
ax.set_title(&quot;(x,y)=(a,a*1.1)&quot;)
print ax.get_xlim()  ## Added for clarity

ax=ax_list[1]
df23[[&quot;b&quot;, &quot;b*1.1&quot;]].plot(ax=ax, x=&quot;b&quot;)
ax.axis('equal')
ax.set_title(&quot;(x,y)=(b,b*1.1)&quot;)
print ax.get_xlim()  ## Added for clarity  

ax=ax_list[2]
df23[[&quot;c&quot;, &quot;c*1.1&quot;]].plot(ax=ax, x=&quot;c&quot;)
ax.axis('equal')
ax.set_title(&quot;(x,y)=(c,c*1.1)&quot;)
print ax.get_xlim()  ## Added for clarity"><pre class="notranslate"><code class="notranslate">import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
df3 = df2*1.1

df3.rename(columns={"a": "a*1.1", "b": "b*1.1", "c": "c*1.1"}, inplace=True)
df23 = df2.join(df3)

fig, ax_list = plt.subplots(1,3)

ax=ax_list[0]
df23[["a", "a*1.1"]].plot(ax=ax, x="a")
ax.axis('equal')
ax.set_title("(x,y)=(a,a*1.1)")
print ax.get_xlim()  ## Added for clarity

ax=ax_list[1]
df23[["b", "b*1.1"]].plot(ax=ax, x="b")
ax.axis('equal')
ax.set_title("(x,y)=(b,b*1.1)")
print ax.get_xlim()  ## Added for clarity  

ax=ax_list[2]
df23[["c", "c*1.1"]].plot(ax=ax, x="c")
ax.axis('equal')
ax.set_title("(x,y)=(c,c*1.1)")
print ax.get_xlim()  ## Added for clarity
</code></pre></div>