简体   繁体   English

Plotly:如何根据列值设置标记大小?

[英]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.

相关问题 Plotly:根据导出数据中的列设置标记大小? - Plotly: Setting the marker size based on the column in the exported data? 基于字符串值的标记颜色 Plotly Python - Marker Color based on String Value Plotly Python 根据 Python 中的第三列值设置 Plotly 条形颜色 - Set Plotly Bar Color Based on 3rd Column Value in Python Map subplot go.pie plotly marker colors using based pandas column value - Map subplot go.pie plotly marker colors using based pandas column value 设置标记大小或不透明度时,Plotly上不会显示3D散点 - 3D scatter points don't show on Plotly when I set marker size or opacity 如何根据交叉点的大小在 Plotly 中构建气泡大小的气泡图? - How can I build a bubble chart in Plotly with bubble size based on how large an intersection is? 如何根据一组条件在 PANDAS 中创建一个新列,然后将新列设置为另一个字段的值 - How can I create a new column in PANDAS based on a set of conditions and then setting the new column to the value of another field 如何在 plotly (python) 中根据我的数据集中的“颜色”列更改散点图点的颜色? - How can I change the color of my scatter plot points based on a "color" column in my dataset in plotly (python)? 基于 pandas 列的 gmaps 标记大小 - gmaps marker size based on pandas column 根据列值更改散点图标记的大小 - Python 3.6.x - Change size of scatterplot marker based on column value - Python 3.6.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM