我最近将matplotlib从3.1.2升级到了3.3.0,并遇到了此错误(我在3.1.2上没有得到):“ ValueError:s必须是标量,或与x和y”。在下面的示例中,我只是试图分散绘制pandas groupby对象,并且所有列都具有相同数量的值和相同尺寸。
import pandas as pd
import numpy as np
import sys, os, time
import matplotlib.pyplot as plt
headers=['Cpl Time','ECT','PendAtStart','OpCode']
tableData=[[1,111.1,11,'Read'],[2,222.2,22,'Write'],[3,333.3,33,'Read'],[4,444.4,44,'Write'],[5,555.5,55,'Read']]
df = pd.DataFrame(tableData,columns=headers)
cols = ['ECT','PendAtStart','Cpl Time']
print("{:20} Shape, Len".format('Column'))
for col in df.columns:
print("{:20} {}, {}".format(col, df[col].shape, len(df[col])))
plt.rcParams['figure.figsize'] = [15,10]
fig, ax = plt.subplots()
currMarker=0
grouped = df.groupby('OpCode')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='Cpl Time', y='ECT', label=key, s=grouped.PendAtStart)
plt.show()
即使我更改该行以尝试对x,y和s使用SAME精确列:
group.plot(ax=ax, kind='scatter', x='ECT', y='ECT', label=key, s=grouped['ECT'])
它仍然返回错误(回溯):
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-195485ba7482> in <module>
6 for key, group in grouped:
7 print("Key:{}".format(key))
----> 8 group.plot(ax=ax, kind='scatter', x='Cpl Time', y='ECT', label=key, s=df.PendAtStart)
9 plt.show()
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
736 if kind in self._dataframe_kinds:
737 if isinstance(data, ABCDataFrame):
--> 738 return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
739 else:
740 raise ValueError(
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\pandas\plotting\_matplotlib\__init__.py in plot(data, kind, **kwargs)
60 kwargs["ax"] = getattr(ax, "left_ax", ax)
61 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 62 plot_obj.generate()
63 plot_obj.draw()
64 return plot_obj.result
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\pandas\plotting\_matplotlib\core.py in generate(self)
279 self._compute_plot_data()
280 self._setup_subplots()
--> 281 self._make_plot()
282 self._add_table()
283 self._make_legend()
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\pandas\plotting\_matplotlib\core.py in _make_plot(self)
967 label=label,
968 cmap=cmap,
--> 969 **self.kwds
970 )
971 if cb:
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1429 def inner(ax, *args, data=None, **kwargs):
1430 if data is None:
-> 1431 return func(ax, *map(sanitize_sequence, args), **kwargs)
1432
1433 bound = new_sig.bind(ax, *args, **kwargs)
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*inner_args, **inner_kwargs)
409 else deprecation_addendum,
410 **kwargs)
--> 411 return func(*inner_args, **inner_kwargs)
412
413 return wrapper
c:\users\klo75358\appdata\local\programs\python\python37\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
4446 s = np.ma.ravel(s)
4447 if len(s) not in (1, x.size):
-> 4448 raise ValueError("s must be a scalar, or the same size as x and y")
4449
4450 c, colors, edgecolors = \
ValueError: s must be a scalar, or the same size as x and y
同样,在Matplotlib 3.1.2中,此代码段生成了一个图形并且没有错误...任何建议/输入将不胜感激!
答案 0 :(得分:0)
s
的大小必须与x
和y
相同。您不能使用s=df.PendAtStart
,因为df
是原始数据帧,并且只需要一组。因此,您需要s=group.PendAtStart
。
请注意,由于熊猫散布图的限制,您不能仅给出列名。参见this github issue。