[英]Plotly: How can I set marker size based on column value?
I am trying to use plotly (version 4.6.0) to create plots, but having trouble with the markers/size attribute.我正在尝试使用 plotly(版本 4.6.0)来创建绘图,但在标记/大小属性方面存在问题。
I am using the Boston housing price dataset in my example.我在示例中使用了波士顿房价数据集。 I want to use the value in one of the columns of my dataframe to set a variable size for the marker, but I get an error when I use a direct reference to the column (
size='TAX'
).我想使用 dataframe 的一列中的值来为标记设置可变大小,但是当我使用对列的直接引用(
size='TAX'
)时出现错误。 I can set the size to a constant ( size=1
) without issues.我可以将大小设置为常量(
size=1
)而不会出现问题。
I found some examples online, but they generate a ValueError
when I try to use them.我在网上找到了一些示例,但是当我尝试使用它们时它们会生成
ValueError
。 How can I avoid this error?我怎样才能避免这个错误? Code and error are shown below.
代码和错误如下所示。
import chart_studio.plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
import cufflinks
cufflinks.go_offline(connected=True)
init_notebook_mode(connected=True)
import pandas as pd
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
y = boston.target
df['RAD_CAT']=df['RAD'].astype(str)
df.iplot(
x='CRIM',
y='INDUS',
size='TAX',
#size=1,
text='RAD',
mode='markers',
layout=dict(
xaxis=dict(type='log', title='CRIM'),
yaxis=dict(title='INDUS'),
title='CRIM vs INDUS Sized by RAD'))
ValueError:
Invalid value of type 'builtins.str' received for the 'size' property of scatter.marker
Received value: 'TAX'
The 'size' property is a number and may be specified as:
- An int or float in the interval [0, inf]
- A tuple, list, or one-dimensional numpy array of the above
import chart_studio.plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
import cufflinks
cufflinks.go_offline(connected=True)
init_notebook_mode(connected=True)
import pandas as pd
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df.iplot(
x='CRIM',
y='INDUS',
size=df['TAX']/20,
text='RAD',
mode='markers',
layout=dict(
xaxis=dict(type='log', title='CRIM'),
yaxis=dict(title='INDUS'),
title='CRIM vs INDUS Sized by TAX'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.