如何用点绘制熊猫数据框的两列?

时间:2022-11-19 01:38:43

I have a pandas data frame and would like to plot values from one column versus the values from another column. Fortunately, there is plot method associated with the data-frames that seems to do what I need:

我有一个熊猫数据框,我想从一列中绘制值,而从另一列中绘制值。幸运的是,有一种与数据帧相关联的绘图方法似乎可以满足我的需要:

df.plot(x='col_name_1', y='col_name_2')

Unfortunately, it looks like among the plot styles (listed here after the kind parameter) there are not points. I can use lines or bars or even density but not points. Is there a work around that can help to solve this problem.

不幸的是,在情节样式(在kind参数后面列出)中似乎没有点。我可以用线,条,甚至密度,但不能用点。有什么工作可以帮助解决这个问题吗?

2 个解决方案

#1


61  

You can specify the style of the plotted line when calling df.plot:

可以在调用df.plot时指定绘制行的样式:

df.plot(x='col_name_1', y='col_name_2', style='o')

The style argument can also be a dict or list, e.g.:

样式参数也可以是一个命令或列表,例如:

import numpy as np
import pandas as pd

d = {'one' : np.random.rand(10),
     'two' : np.random.rand(10)}

df = pd.DataFrame(d)

df.plot(style=['o','rx'])

All the accepted style formats are listed in the documentation of matplotlib.pyplot.plot.

所有可接受的样式格式都列在matplotlib.pyplot的文档中。

如何用点绘制熊猫数据框的两列?

#2


47  

For this (and most plotting) I would not rely on the Pandas wrappers to matplotlib. Instead, just use matplotlib directly:

对于这个(也是大多数的绘图),我不会依赖熊猫的包装来进行matplotlib。相反,直接使用matplotlib:

import matplotlib.pyplot as plt
plt.scatter(df['col_name_1'], df['col_name_2'])
plt.show() # Depending on whether you use IPython or interactive mode, etc.

and remember that you can access a NumPy array of the column's values with df.col_name_1.values for example.

记住,可以使用df.col_name_1访问列值的NumPy数组。值的例子。

I ran into trouble using this with Pandas default plotting in the case of a column of Timestamp values with millisecond precision. In trying to convert the objects to datetime64 type, I also discovered a nasty issue: < Pandas gives incorrect result when asking if Timestamp column values have attr astype >.

我在使用熊猫默认绘图时遇到了麻烦,如果是一列具有毫秒精度的时间戳值。在尝试将对象转换为datetime64类型时,我还发现了一个棘手的问题:< panda在询问时间戳列值是否具有attr astype >时给出了错误的结果。

#1


61  

You can specify the style of the plotted line when calling df.plot:

可以在调用df.plot时指定绘制行的样式:

df.plot(x='col_name_1', y='col_name_2', style='o')

The style argument can also be a dict or list, e.g.:

样式参数也可以是一个命令或列表,例如:

import numpy as np
import pandas as pd

d = {'one' : np.random.rand(10),
     'two' : np.random.rand(10)}

df = pd.DataFrame(d)

df.plot(style=['o','rx'])

All the accepted style formats are listed in the documentation of matplotlib.pyplot.plot.

所有可接受的样式格式都列在matplotlib.pyplot的文档中。

如何用点绘制熊猫数据框的两列?

#2


47  

For this (and most plotting) I would not rely on the Pandas wrappers to matplotlib. Instead, just use matplotlib directly:

对于这个(也是大多数的绘图),我不会依赖熊猫的包装来进行matplotlib。相反,直接使用matplotlib:

import matplotlib.pyplot as plt
plt.scatter(df['col_name_1'], df['col_name_2'])
plt.show() # Depending on whether you use IPython or interactive mode, etc.

and remember that you can access a NumPy array of the column's values with df.col_name_1.values for example.

记住,可以使用df.col_name_1访问列值的NumPy数组。值的例子。

I ran into trouble using this with Pandas default plotting in the case of a column of Timestamp values with millisecond precision. In trying to convert the objects to datetime64 type, I also discovered a nasty issue: < Pandas gives incorrect result when asking if Timestamp column values have attr astype >.

我在使用熊猫默认绘图时遇到了麻烦,如果是一列具有毫秒精度的时间戳值。在尝试将对象转换为datetime64类型时,我还发现了一个棘手的问题:< panda在询问时间戳列值是否具有attr astype >时给出了错误的结果。