bug: plot two lines, unordered xlabels with type str · Issue #18687 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
Your code here
%matplotlib inline
import pandas as pd df1 = pd.DataFrame([{'x': '1', 'y': 1}, {'x': '2', 'y': 2}]) df2 = pd.DataFrame([{'x': '2', 'y': 3}, {'x': '1', 'y': 4}])
print(df1)
x y
0 1 1
1 2 2
print(df2)
x y
0 2 3
1 1 4
Wrong plot
ax = None ax = df1.plot('x', 'y', ax=ax) ax = df2.plot('x', 'y', ax=ax)
Correct plot
ax = None ax = df1.sort_values(by=['x']).plot('x', 'y', ax=ax) ax = df2.sort_values(by=['x']).plot('x', 'y', ax=ax)
Problem description
I want to plot multiple dataframes in one graph. The x values are strings. The x value order in both dataframes is different.
The first plot draws the line
x = ['1', '2']
andy = [1, 2]
The second plot draws the linex = ['2', '1']
andy = [4, 3]
Since the second plot overwrites the xticks, the first line is nowx = ['2', '1']
andy = [1, 2]
.
Expected Output
Output of pd.show_versions()
[paste the output of pd.show_versions()
here below this line]
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-38-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8
pandas: 0.21.0
pytest: 3.3.0
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None