python - 在 python plt.plot 中使用系列作为标记大小

标签 python pandas matplotlib

是否可以使用数据框中的列来缩放 matplotlib 中的标记大小?当我执行以下操作时,我不断收到有关使用系列的错误消息。

import pandas as pd
import matplotlib.pyplot as plt

my_dict = {'Vx': [16,25,85,45], 'r': [1315,5135,8444,1542], 'ms': [10,50,100, 25]}
df= pd.DataFrame(my_dict)
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.plot(df.Vx, df.r, '.', markersize= df.ms)

当我运行时

ValueError: setting an array element with a sequence.

我猜它不喜欢我向标记器提供系列的事实,但必须有一种方法让它工作......

最佳答案

使用plt.scatter而不是 plt.plot。 Scatter 允许您使用元组或列表指定大小 s 以及点的颜色 c

import pandas as pd
import matplotlib.pyplot as plt

my_dict = {'Vx': [16,25,85,45], 'r': [1315,5135,8444,1542], 'ms': [10,50,100, 25]}
df= pd.DataFrame(my_dict)
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.scatter(df.Vx, df.r, s= df.ms)
plt.show()

关于python - 在 python plt.plot 中使用系列作为标记大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41104302/

相关文章:

python - 将列表的数据框 reshape 为另一个数据框

Python Pyinstaller Matplotlibrc

python - matplotlib 函数图的彩色部分

python - 号码流检测

python - 我什么时候应该在 Pandas 数据帧上使用 query vs eval?

python - 如何从 Python 中的 csv 文件生成 JSON?

python - Matplotlib 挑战 - 仅在存在范围内绘制的智能方法

python - 不允许同时指定 'fields' 和 'form_class'

python - 带有 postgresql 的 SQLAlchemy 产生错误 "Could not locate column in row for column ' attype'"

python - 同一个对象的不同内存地址