1

Say I have the dataframe below:

import pandas as pd
data = {'Col1':['(-2.0, 1.0]', '(1.0, 4.0]', '(4.0, 6.0]', '(6.0, 9.0]', '(9.0, 11.0]', '(11.0, 14.0]', '(14.0, 16.0]', '(16.0, 19.0]', '(19.0, 21.0]', '(21.0, 24.0]'],
        'Col2':[3.409836, 2.930693, 2.75, 3.140845, 2.971429, 2.592593, 2.6, 3.1875, 2.857143, 0.714286]}
df = pd.DataFrame(data, columns=['Col1', 'Col2'])
df

enter image description here

I want to plot df.Col2 against df.Col1. But since Col1 contains ranges or bins of something, the Col1 values are not float or int - they are strings. Thus, the plot does not show the x-axis in order:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,5))
plt.plot([str(i) for i in df.Col2], df.Col1)

enter image description here

How do I fix this?

EDIT: For multiple subplots I can't use df.plot(x='Col1',y='Col2') because I have this plot as one of the subplots:

df1 = pd.DataFrame(data, columns=['Col1', 'Col2'])
df2 = df1
df3 = df1

fig = plt.figure(figsize=(20,5))

plt.subplot(1,3,1)
plt.plot([str(i) for i in df1.Col1], df1.Col2)

plt.subplot(1,3,2)
plt.plot([str(i) for i in df2.Col1], df2.Col2)

plt.subplot(1,3,3)
plt.plot([str(i) for i in df3.Col1], df3.Col2)

enter image description here

I tried the following:

fig, axes = plt.subplots(nrows=1, ncols=3)

plt.subplot(1,3,1)
df1.plot(x='Col1',y='Col2',ax=axes[0,0])

plt.subplot(1,3,2)
df2.plot(x='Col1',y='Col2',ax=axes[0,1])

plt.subplot(1,3,3)
df3.plot(x='Col1',y='Col2',ax=axes[0,2])

But got this error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-113-b0fcf5cd6711> in <module>()
      2 
      3 plt.subplot(1,3,1)
----> 4 df1.plot(x='Col1',y='Col2',ax=axes[0,0])
      5 
      6 plt.subplot(1,3,2)

IndexError: too many indices for array

And I get the exact same error for this as well:

fig, axes = plt.subplots(nrows=1, ncols=3)

df1.plot(x='Col1',y='Col2',ax=axes[0,0])
df2.plot(x='Col1',y='Col2',ax=axes[0,1])
df3.plot(x='Col1',y='Col2',ax=axes[0,2])

EDIT 2: Ok, I came across the first comment of this answer, and the following works:

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(20,5))

df1.plot(x='Col1',y='Col2',ax=axes[0])
df2.plot(x='Col1',y='Col2',ax=axes[1])
df3.plot(x='Col1',y='Col2',ax=axes[2])

enter image description here


EDIT 3: For plotting the 3 dataframes in one plot

ax = df1.plot(x='Col1',y='Col2')
df2.plot(x='Col1',y='Col2',ax=ax)
df3.plot(x='Col1',y='Col2',ax=ax)

enter image description here

Kristada673
  • 3,512
  • 6
  • 39
  • 93

1 Answers1

1

Solution:

df.plot(x='Col1',y='Col2')

Example:

Try using pandas plotting function:

import pandas as pd
data = {'Col1':['(-2.0, 1.0]', '(1.0, 4.0]', '(4.0, 6.0]', '(6.0, 9.0]', '(9.0, 11.0]', '(11.0, 14.0]', '(14.0, 16.0]', '(16.0, 19.0]', '(19.0, 21.0]', '(21.0, 24.0]'],
        'Col2':[3.409836, 2.930693, 2.75, 3.140845, 2.971429, 2.592593, 2.6, 3.1875, 2.857143, 0.714286]}
df = pd.DataFrame(data)
import matplotlib.pyplot as plt
df.plot(x='Col1',y='Col2')
plt.show()

Output:

Plot Image

For subplots:

import pandas as pd
data = {'Col1':['(-2.0, 1.0]', '(1.0, 4.0]', '(4.0, 6.0]', '(6.0, 9.0]', '(9.0, 11.0]', '(11.0, 14.0]', '(14.0, 16.0]', '(16.0, 19.0]', '(19.0, 21.0]', '(21.0, 24.0]'],
        'Col2':[3.409836, 2.930693, 2.75, 3.140845, 2.971429, 2.592593, 2.6, 3.1875, 2.857143, 0.714286]}
import matplotlib.pyplot as plt
df = pd.DataFrame(data, columns=['Col1', 'Col2'])

fig, axes = plt.subplots(ncols=3,figsize=(20,5))


df.plot(x='Col1',y='Col2',ax=axes[0])


df.plot(x='Col1',y='Col2',ax=axes[1])


df.plot(x='Col1',y='Col2',ax=axes[2])
plt.show()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114