作为 Python 类(class)的一部分,我正在尝试使用 DataFrame 的 Plotly 创建一个图表。 DataFrame 是从本地 csv 文件创建的,运行良好。
cars = pd.read_csv(r'C:\Users\arman\OneDrive\Desktop\mtcars.csv')
cars.columns = ['car_names','mpg','cyl','disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb']
df = cars[['cyl', 'wt','mpg']]
layouts = dict(title = 'Chart From Pandas DataFrame', xaxis= dict(title='x-axis'), yaxis= dict(title='y-axis'))
df.iplot(filename='cf-simple-line-chart', layout= layouts)
运行此程序时,出现以下错误:
ValueErrorTraceback (most recent call last)
<ipython-input-9-9d51de7f2e8a> in <module>()
6 cars_select.columns = ['mpg', 'disp', 'hp']
7
----> 8 cars_select.iplot(kind='histogram', filename='multiple-histogram-chart')
C:\Users\arman\AppData\Local\Continuum\anaconda2\lib\site-packages\cufflinks\plotlytools.pyc in _iplot(self, data, layout, filename, sharing, kind, title, xTitle, yTitle, zTitle, theme, colors, colorscale, fill, width, dash, mode, symbol, size, barmode, sortbars, bargap, bargroupgap, bins, histnorm, histfunc, orientation, boxpoints, annotations, keys, bestfit, bestfit_colors, mean, mean_colors, categories, x, y, z, text, gridcolor, zerolinecolor, margin, labels, values, secondary_y, subplots, shape, error_x, error_y, error_type, locations, lon, lat, asFrame, asDates, asFigure, asImage, dimensions, asPlot, asUrl, online, **kwargs)
899 ## Figure defintion
900 figure=Figure()
--> 901 figure['data']=data
902 figure['layout']=layout
903
C:\Users\arman\AppData\Local\Continuum\anaconda2\lib\site-packages\plotly\basedatatypes.pyc in __setitem__(self, prop, value)
234
235 if prop == 'data':
--> 236 self.data = value
237 elif prop == 'layout':
238 self.layout = value
C:\Users\arman\AppData\Local\Continuum\anaconda2\lib\site-packages\plotly\basedatatypes.pyc in __setattr__(self, prop, value)
266 if prop.startswith('_') or hasattr(self, prop):
267 # Let known properties and private properties through
--> 268 super(BaseFigure, self).__setattr__(prop, value)
269 else:
270 # Raise error on unknown public properties
C:\Users\arman\AppData\Local\Continuum\anaconda2\lib\site-packages\plotly\basedatatypes.pyc in data(self, new_data)
439 .format(invalid_uids=invalid_uids))
440
--> 441 raise ValueError(err_msg)
442
443 # ### Check for duplicates in assignment ###
ValueError: The data property of a figure may only be assigned a list or tuple that contains a permutation of a subset of itself
Invalid trace(s) with uid(s): set([None])
任何帮助将不胜感激。
编辑:我已经发布了示例数据集的图像(不确定发布 csv 文件的最佳格式;但如果有帮助,我从 Lynda.com 上的 Python for Data Science Essential Training 类(class)中获取了此文件) mtcars csv file
我还尝试使用以下数据帧创建绘图:
df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'
df.scatter_matrix(filename='cufflinks/scatter-matrix', world_readable=True)
和
df = cf.datagen.lines()
df.iplot(kind='scatter', filename='cufflinks/cf-simple-line')
两个示例 DataFrame 都会产生相同的错误。我假设这可能是袖扣的问题。我目前运行的是0.8.2版本。
最佳答案
感谢大家的帮助。发现问题出在安装Plotly时,它安装了3.1.1版本而不是2.7.0,产生了这个错误。在重新安装 2.7.0 版本时,我能够绘制我想要绘制的图表。
关于python - 将 Pandas 对象与 Plotly ValueError 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058221/