Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当轴点为面元时,如何在matplotlib中对轴进行排序?_Python_Pandas_Matplotlib_Plot_Graph - Fatal编程技术网

Python 当轴点为面元时,如何在matplotlib中对轴进行排序?

Python 当轴点为面元时,如何在matplotlib中对轴进行排序?,python,pandas,matplotlib,plot,graph,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.14084

假设我有下面的数据框:

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

我想根据
df.Col1
绘制
df.Col2
。但是由于
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)

我该如何解决这个问题

编辑:对于多个子绘图我不能使用
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)

我尝试了以下方法:

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

编辑2:好的,我看到了答案的第一条评论,以及以下作品:

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


编辑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)

解决方案: 例子: 尝试使用以下功能:

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()
输出:

对于子地块:
@Kristada673是预期的图:@U9 Forward是的,这是预期的图,x轴点是有序的。@Kristada673好的,good@Kristada673你所说的有序是什么意思?你所要做的就是
df.plot(x='Col1',y='Col2')
。它将按顺序绘制。对于在子情节场景中寻求解决方案的其他人,我修改了U9 Forward的答案,并将其放在问题的编辑2中。@Kristada673哈哈。你做的链接我也看过了,我也在为你做同样的解决方案you@Kristada673另一个问题是,我无法让它与循环中生成的数据帧一起工作。如果您有时间,请看一下这里:
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()
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()