Plotly:如何根据列值设置标记大小?
Plotly: How can I set marker size based on column value?
Hi,
I am trying to use plotly (version 4.6.0) to create plots, but having trouble with the markers/size attribute. 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'). I can set the size to a constant (size=1) without issues. I found some examples online, but they generate a "ValueError: ..." error when I try to use them. 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'))
Hi,
I am trying to use plotly (version 4.6.0) to create plots, but having trouble with the markers/size attribute. 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'). I can set the size to a constant (size=1) without issues. I found some examples online, but they generate a "ValueError: ..." error when I try to use them. 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'))