当轴点为仓时,如何在matplotlib中对轴排序?

时间:2018-08-21 03:08:57

标签: python pandas matplotlib plot graph

说我有以下数据框:

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

我想针对df.Col2绘制df.Col1。但是,由于Col1包含某物的范围或箱,因此Col1的值不是float或int,而是字符串。因此,该图未按顺序显示x轴:

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

我该如何解决?

编辑:对于多个子图,我不能使用df.plot(x='Col1',y='Col2'),因为我将此图作为子图之一:

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

我尝试了以下操作:

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])

但是出现此错误:

---------------------------------------------------------------------------
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

我也得到了完全相同的错误:

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])

编辑2:好吧,我遇到了this答案的第一条评论,并且有以下作品:

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


编辑3:用于在一个图中绘制3个数据框

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

1 个答案:

答案 0 :(得分:1)

解决方案:

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

示例:

尝试使用熊猫绘图功能:

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()

输出:

Plot Image

对于子图:

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()