Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python plotly:不固定的跟踪数_Python_Plotly_Openpyxl - Fatal编程技术网

python plotly:不固定的跟踪数

python plotly:不固定的跟踪数,python,plotly,openpyxl,Python,Plotly,Openpyxl,我的代码从.xlsx文件中读取数据,并使用plotly绘制气泡图。 当我知道需要绘制多少条记录道时,任务很容易。然而,由于行的数量是可变的,当记录道的数量不固定时,我陷入了困惑 1991 1992 1993 1994 1995 1996 1997 US 10 14 16 18 20 42 64 JAPAN 100 30 70 85 30 42 64 CN 50 2

我的代码从.xlsx文件中读取数据,并使用plotly绘制气泡图。 当我知道需要绘制多少条记录道时,任务很容易。然而,由于行的数量是可变的,当记录道的数量不固定时,我陷入了困惑


       1991  1992  1993  1994  1995  1996  1997
US       10    14    16    18    20    42    64
JAPAN   100    30    70    85    30    42    64
CN       50    22    30    65    70    66    60
这是我未完成的代码:

# Version 2 could read data from .xlsx file.
import plotly as py
import plotly.graph_objs as go
import openpyxl

wb = openpyxl.load_workbook(('grape output.xlsx'))     
sheet = wb['Sheet1']       
row_max = sheet.max_row
col_max = sheet.max_column
l=[]

for row_n in range(row_max-1):
    l.append([])
    for col_n in range(col_max-1):
        l[row_n].append(sheet.cell(row=row_n+2, column=col_n+2).value)

trace0 = go.Scatter(
    x=[1991, 1992, 1993, 1994, 1995, 1996, 1997],
    y=['US', 'US', 'US', 'US', 'US', 'US', 'US'],
    mode='markers+text',
    marker=dict(
        color='rgb(150,204,90)',
        size= l[0],
        showscale = False,
        ),
    text=list(map(str, l[0])),     
    textposition='middle center',   
)

trace1 = go.Scatter(
    x=[1991, 1992, 1993, 1994, 1995, 1996, 1997],
    y=['JAPAN', 'JAPAN', 'JAPAN', 'JAPAN', 'JAPAN', 'JAPAN', 'JAPAN'],
    mode='markers+text',

    marker=dict(
        color='rgb(255, 130, 71)',
        size=l[1],
        showscale=False,
    ),
    text=list(map(str,l[1])),
    textposition='middle center',
)

trace2 = go.Scatter(
    x=[1991, 1992, 1993, 1994, 1995, 1996, 1997],
    y=['CN', 'CN', 'CN', 'CN', 'CN', 'CN', 'CN'],
    mode='markers+text',

    marker=dict(
        color='rgb(255, 193, 37)',
        size=l[2],
        showscale=False,
    ),
    text=list(map(str,l[2])),
    textposition='middle center',
)

layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',  
                   paper_bgcolor='rgb(20, 55, 100)',  
                   font={               
                       'size': 15,
                       'family': 'sans-serif',
                       'color': 'rgb(255, 255, 255)'  
                   },
                   width=1000,
                   height=500,
                   xaxis=dict(title='Output of grapes per year in US, JAPAN and CN', ),  
                   showlegend=False,
                   margin=dict(l=100, r=100, t=100, b=100),
                   hovermode = False,       
                   )

data = [trace0, trace1, trace2]
fig = go.Figure(data=data, layout=layout)


py.offline.init_notebook_mode()
py.offline.plot(fig, filename='basic-scatter.html')


你能教我怎么画吗?Thx

我应该指出,如果您将原始数据作为文本或更容易复制和粘贴的内容附加,那么您的代码将更具可复制性。然而,我仍然可以回答你的问题,并为你指出正确的方向

您应该做的是使用一个循环,从查看行
data=[trace0,trace1,trace2]
开始。正如你所注意到的,如果你有100个国家而不是3个,这种方法将不会扩大

相反,您可以使用列表理解将
数据创建为列表,并更新每个跟踪中发生更改的部分
trace0
trace1
trace2
除了国家、值和颜色外,没有太大的不同。为了向您展示我的意思,我使用数据框重新创建了您的数据,然后创建了包含您所在国家和颜色的单独列表

# Version 2 could read data from .xlsx file.
import plotly as py
import plotly.graph_objs as go
import openpyxl

# wb = openpyxl.load_workbook(('grape output.xlsx'))     
# sheet = wb['Sheet1']       
# row_max = sheet.max_row
# col_max = sheet.max_column
# l=[]

# for row_n in range(row_max-1):
#     l.append([])
#     for col_n in range(col_max-1):
#         l[row_n].append(sheet.cell(row=row_n+2, column=col_n+2).value)

import pandas as pd

df = pd.DataFrame({1991:[10,100,50], 1992:[14,30,22], 1993:[16,70,30], 1994:[18,85,65], 1995:[20,30,70], 1996:[42,42,66], 1997:[64,64,60]})
df.index = ['US','JAPAN','CN']
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)']

data = [go.Scatter(
    x=df.columns,
    y=[country]*len(df.columns),
    mode='markers+text',
    marker=dict(
        color=colors[num],
        size= df.loc[country],
        showscale = False,
        ),
    text=list(map(str, df.loc[country])),     
    textposition='middle center',   
    )
    for num, country in enumerate(df.index)
]

layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',  
                   paper_bgcolor='rgb(20, 55, 100)',  
                   font={               
                       'size': 15,
                       'family': 'sans-serif',
                       'color': 'rgb(255, 255, 255)'  
                   },
                   width=1000,
                   height=500,
                   xaxis=dict(title='Output of grapes per year in US, JAPAN and CN', ),  
                   showlegend=False,
                   margin=dict(l=100, r=100, t=100, b=100),
                   hovermode = False,       
                   )

# data = [trace0, trace1, trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()

# py.offline.init_notebook_mode()
# py.offline.plot(fig, filename='basic-scatter.html')
如果我随后使用1991-1997的值向数据框添加一个测试国家,我不需要更改代码的其余部分,气泡图将相应地更新

# I added a test country with data
df = pd.DataFrame({1991:[10,100,50,10], 1992:[14,30,22,20], 1993:[16,70,30,30], 1994:[18,85,65,40], 1995:[20,30,70,50], 1996:[42,42,66,60], 1997:[64,64,60,70]})
df.index = ['US','JAPAN','CN','TEST']
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(100, 100, 100)']

我应该指出,如果您将原始数据附加为文本或更容易复制和粘贴的内容,您的代码将更具可复制性。然而,我仍然可以回答你的问题,并为你指出正确的方向

您应该做的是使用一个循环,从查看行
data=[trace0,trace1,trace2]
开始。正如你所注意到的,如果你有100个国家而不是3个,这种方法将不会扩大

相反,您可以使用列表理解将
数据创建为列表,并更新每个跟踪中发生更改的部分
trace0
trace1
trace2
除了国家、值和颜色外,没有太大的不同。为了向您展示我的意思,我使用数据框重新创建了您的数据,然后创建了包含您所在国家和颜色的单独列表

# Version 2 could read data from .xlsx file.
import plotly as py
import plotly.graph_objs as go
import openpyxl

# wb = openpyxl.load_workbook(('grape output.xlsx'))     
# sheet = wb['Sheet1']       
# row_max = sheet.max_row
# col_max = sheet.max_column
# l=[]

# for row_n in range(row_max-1):
#     l.append([])
#     for col_n in range(col_max-1):
#         l[row_n].append(sheet.cell(row=row_n+2, column=col_n+2).value)

import pandas as pd

df = pd.DataFrame({1991:[10,100,50], 1992:[14,30,22], 1993:[16,70,30], 1994:[18,85,65], 1995:[20,30,70], 1996:[42,42,66], 1997:[64,64,60]})
df.index = ['US','JAPAN','CN']
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)']

data = [go.Scatter(
    x=df.columns,
    y=[country]*len(df.columns),
    mode='markers+text',
    marker=dict(
        color=colors[num],
        size= df.loc[country],
        showscale = False,
        ),
    text=list(map(str, df.loc[country])),     
    textposition='middle center',   
    )
    for num, country in enumerate(df.index)
]

layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',  
                   paper_bgcolor='rgb(20, 55, 100)',  
                   font={               
                       'size': 15,
                       'family': 'sans-serif',
                       'color': 'rgb(255, 255, 255)'  
                   },
                   width=1000,
                   height=500,
                   xaxis=dict(title='Output of grapes per year in US, JAPAN and CN', ),  
                   showlegend=False,
                   margin=dict(l=100, r=100, t=100, b=100),
                   hovermode = False,       
                   )

# data = [trace0, trace1, trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()

# py.offline.init_notebook_mode()
# py.offline.plot(fig, filename='basic-scatter.html')
如果我随后使用1991-1997的值向数据框添加一个测试国家,我不需要更改代码的其余部分,气泡图将相应地更新

# I added a test country with data
df = pd.DataFrame({1991:[10,100,50,10], 1992:[14,30,22,20], 1993:[16,70,30,30], 1994:[18,85,65,40], 1995:[20,30,70,50], 1996:[42,42,66,60], 1997:[64,64,60,70]})
df.index = ['US','JAPAN','CN','TEST']
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(100, 100, 100)']

Derek O.的答案很完美,但我认为有一种更灵活的方法可以使用
绘图。如果您不想定义颜色,请特别表达

其思想是正确地转换数据

资料
将熊猫作为pd导入
df=pd.数据帧({1991:[10100,50],1992:[14,30,22],1993:[16,70,30],1994:[18,85,65],1995:[20,30,70],1996:[42,42,66],1997:[64,64,60]})
df.index=[“美国”、“日本”、“中国”]
df=df.T.取消堆叠()\
.reset_index()\
.rename(列={“0级”:“国家”,
“1级”:“年”,
0:“n”})
打印(df)
使用
plotly.express
既然您的数据是长格式的,您就可以使用
plotly.express
,如下所示

将plotly.express导入为px
图=像素散射(df,
x=“年”,
y=“国家”,
size=“n”,
color=“国家”,
text=“n”,
size_max=50#你需要这个,否则气泡太小
)
图1更新布局图(绘图\u bgcolor='rgb(10,10,10)',
纸张颜色为rgb(20,55,100),
字体={'size':15,
“家庭”:“无衬线”,
“颜色”:“rgb(255、255、255)”
},
宽度=1000,
高度=500,
xaxis=dict(title='所选国家每年的葡萄产量',),
showlegend=False,
裕度=dict(l=100,r=100,t=100,b=100),
hovermode=False,)
#如果您不想将国家作为yaxis标题,请取消对此的注释
#fig.layout.yaxis.title.text=无
图2(图3)

Derek O.的答案很完美,但我认为有一种更灵活的方法可以使用
绘图。如果您不想定义颜色,请特别表达

其思想是正确地转换数据

资料
将熊猫作为pd导入
df=pd.数据帧({1991:[10100,50],1992:[14,30,22],1993:[16,70,30],1994:[18,85,65],1995:[20,30,70],1996:[42,42,66],1997:[64,64,60]})
df.index=[“美国”、“日本”、“中国”]
df=df.T.取消堆叠()\
.reset_index()\
.rename(列={“0级”:“国家”,
“1级”:“年”,
0:“n”})
打印(df)
使用
plotly.express
既然您的数据是长格式的,您就可以使用
plotly.express
,如下所示

将plotly.express导入为px
图=像素散射(df,
x=“年”,
y=“国家”,
size=“n”,
color=“国家”,
text=“n”,
size_max=50#你需要这个,否则气泡太小
)
图1更新布局图(绘图\u bgcolor='rgb(10,10,10)',
纸张颜色为rgb(20,55,100),
字体={'size':15,
“家庭”:“无衬线”,
“颜色”:“rgb(255、255、255)”
},
宽度=1000,
高度=500,
xaxis=dict(title='所选国家每年的葡萄产量',),
showlegend=False,
裕度=dict(l=100,r=100,t=100,b=100),
hovermode=False,)
#如果您不想将国家作为yaxis标题,请取消对此的注释
#fig.layout.yaxis.title.text=无
图2(图3)

代码已更新至第2版,可以从.xlsx文件读取数据并绘制气泡直径
# Version 2 
import plotly as py
import plotly.graph_objs as go
import openpyxl
import pandas as pd


wb = openpyxl.load_workbook('grape output.xlsx')
sheet = wb['Sheet1']
row_max = sheet.max_row
col_max = sheet.max_column
first_row_list = []
first_col_list = []
for col_n in range(2, col_max+1):
    first_row_list.append(sheet.cell(row=1, column=col_n).value)
for row_n in range(2,row_max+1):
    first_col_list.append(sheet.cell(row=row_n, column=1).value)

data_all = pd.read_excel('grape output.xlsx')
data = data_all.loc[:,first_row_list]

df = pd.DataFrame(data)
df.index = first_col_list
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(180,240,190)','rgb(255, 10, 1)',
          'rgb(25, 19, 3)','rgb(100, 100, 100)','rgb(45,24,200)','rgb(33, 58, 108)','rgb(35, 208, 232)']

data = [go.Scatter(
    x=df.columns,
    y=[country]*len(df.columns),
    mode='markers+text',
    marker=dict(
        color=colors[num],
        size= df.loc[country],
        showscale = False,
        ),
    text=list(map(str, df.loc[country])),
    textposition='middle center',
    )
    for num, country in enumerate(reversed(df.index))
]

layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',
                   paper_bgcolor='rgb(20, 55, 100)',
                   font={
                       'size': 15,
                       'family': 'sans-serif',
                       'color': 'rgb(255, 255, 255)'
                   },
                   width=1000,
                   height=800,
                   xaxis=dict(title='Output of grapes per year in US, JAPAN and CN'),
                   showlegend=False,
                   margin=dict(l=100, r=100, t=100, b=100),
                   hovermode = False,
                   )

fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='basic-scatter.html')