在Python的ggplot中,使用离散X比例和geom_point()?
下面的示例返回一个错误。在Python的ggplot中,似乎不支持对x轴使用离散(非连续)比例在Python的ggplot中,使用离散X比例和geom_point()?,python,matplotlib,ggplot2,scatter,python-ggplot,Python,Matplotlib,Ggplot2,Scatter,Python Ggplot,下面的示例返回一个错误。在Python的ggplot中,似乎不支持对x轴使用离散(非连续)比例 import pandas as pd import ggplot df = pd.DataFrame.from_dict({'a':['a','b','c'], 'percentage':[.1,.2,.3]}) p = ggplot.ggplot(data=df, aesthetics=ggplot.aes(x='a',
import pandas as pd
import ggplot
df = pd.DataFrame.from_dict({'a':['a','b','c'],
'percentage':[.1,.2,.3]})
p = ggplot.ggplot(data=df,
aesthetics=ggplot.aes(x='a',
y='percentage'))\
+ ggplot.geom_point()
print(p)
如前所述,这将返回:
Traceback (most recent call last):
File "/Users/me/Library/Preferences/PyCharm2016.1/scratches/scratch_1.py", line 30, in <module>
print(p)
File "/Users/me/lib/python3.5/site-packages/ggplot/ggplot.py", line 116, in __repr__
self.make()
File "/Users/me/lib/python3.5/site-packages/ggplot/ggplot.py", line 627, in make
layer.plot(ax, facetgroup, self._aes, **kwargs)
File "/Users/me/lib/python3.5/site-packages/ggplot/geoms/geom_point.py", line 60, in plot
ax.scatter(x, y, **params)
File "/Users/me/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner
return func(ax, *args, **kwargs)
File "/Users/me/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3838, in scatter
x, y, s, c = cbook.delete_masked_points(x, y, s, c)
File "/Users/me/lib/python3.5/site-packages/matplotlib/cbook.py", line 1848, in delete_masked_points
raise ValueError("First argument must be a sequence")
ValueError: First argument must be a sequence
回溯(最近一次呼叫最后一次):
文件“/Users/me/Library/Preferences/PyCharm2016.1/scratches/scratch_1.py”,第30行,在
印刷品(p)
文件“/Users/me/lib/python3.5/site packages/ggplot/ggplot.py”,第116行,在__
self.make()
文件“/Users/me/lib/python3.5/site packages/ggplot/ggplot.py”,第627行,在make中
图层打印(ax、facetgroup、self._aes、**kwargs)
文件“/Users/me/lib/python3.5/site packages/ggplot/geoms/geom_point.py”,第60行,在plot中
最大散射(x,y,**参数)
文件“/Users/me/lib/python3.5/site packages/matplotlib/_init__.py”,第1819行,内部
返回函数(ax,*args,**kwargs)
文件“/Users/me/lib/python3.5/site packages/matplotlib/axes/_axes.py”,第3838行,分散显示
x、 y,s,c=cbook。删除屏蔽点(x,y,s,c)
文件“/Users/me/lib/python3.5/site packages/matplotlib/cbook.py”,第1848行,在delete\u masked\u points中
raise VALUERROR(“第一个参数必须是序列”)
ValueError:第一个参数必须是序列
使用
ggplot
和离散比例上的散射体有什么解决方法吗 一个选项是生成连续序列,并使用原始变量作为标签。但这似乎是一个痛苦的解决办法
df = pd.DataFrame.from_dict( {'a':[0,1,2],
'a_name':['a','b','c'],
'percentage':[.1,.2,.3]})
p = ggplot.ggplot(data=df,
aesthetics=ggplot.aes(x='a',
y='percentage'))\
+ ggplot.geom_point()\
+ ggplot.scale_x_continuous(breaks=list(df['a']),
labels=list(df['a_name']))
当我试图绘制一个数据帧的两列时,我也遇到了同样的错误。我从csv文件中读取数据并将其转换为数据帧
readdata=csv.reader(open(filename),delimiter="\t")
df= pd.DataFrame(data, columns=header)
df.columns=["pulseVoltage","dutVoltage","dutCurrent","leakageCurrent"]
print (df.dtypes)
当我检查数据类型时,出于某种原因,它们显示为对象,而不是我所期望的浮点(我是一个新手,这可能是我不知道的琐碎知识)。因此,我继续进行并将列显式转换为数据类型float
df["dutVoltage"]=df["dutVoltage"].astype("float")
df["dutCurrent"]=df["dutCurrent"].astype("float")
现在我可以使用ggplot来绘制数据,而不会出现任何错误
print ggplot(df, aes('dutVoltage','dutCurrent'))+ \
geom_point()