python - ValueError : Invalid property specified for object of type plotly. graph_objs.Pie: 'xaxis'

标签 python plot plotly visualization subplot

我正在尝试在 2 个不同的轴上绘制 2 个不同的 Plotly 饼图。这是我的代码:

fig = tools.make_subplots(rows=1, cols=2)

monday_freq = meta_df['monday_is_day_off'].value_counts()
tuesday_freq = meta_df['tuesday_is_day_off'].value_counts()

trace1 = go.Pie(labels=list(monday_freq.keys()), 
values=list(monday_freq.values), hole=0.7, hoverinfo='label+percent')
trace2 = go.Pie(labels=list(tuesday_freq.keys()), 
values=list(tuesday_freq.values), hole=0.7, hoverinfo='label+percent')

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)

iplot(fig)

当我运行它时,出现以下错误:

ValueError: Invalid property specified for object of type plotly.graph_objs.Pie: 'xaxis'

我一直在网上搜索解决方案,但无济于事。

最佳答案

还有另一种绘制饼图子图的方法。你只需要指定domainannotation(如果你想在饼图的洞中放置迹线的名称)。

代码(基于 plotly 文档中的示例数据):

# import all the necessaries libraries
import plotly.offline as py
import plotly.graph_objs as go

# change to your data
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
labels2 = ['Helium','Lithium','Aluminium','Phosphorus']
values = [4500,2500,1053,500]
values2 = [6500,1500,2700,1286]
colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']
colors2 = ['#29ECEC', '#EC7029', '#5E6B6B','#B6E44C']

# Create one trace for each day (Monday, Tuesday and so on)
trace1 = go.Pie(labels=labels, values=values,
               hoverinfo='label+percent', textinfo='value', 
               textfont=dict(size=20),
               name = 'Monday',
               # Create hole in pie where we will place day name
               hole = 0.2,
               marker=dict(colors=colors, 
                           line=dict(color='#000000', width=2)
                           ),
               # Set where first plot will be plotted
               domain=dict(x=[0,0.5])
               )
trace2 = go.Pie(labels=labels2, values=values2,
               hoverinfo='label+percent', textinfo='value', 
               textfont=dict(size=20),
               name='Tuesday',
               # Create hole in pie where we will place day name
               hole = 0.2,
               marker=dict(colors=colors2, 
                           line=dict(color='#000000', width=2)
                           ),
               # Set where second plot will be plotted
               domain=dict(x=[0.5,1.0])
               )
# Fill out the data wtih traces
data = [trace1,trace2]
# Create one annotation to each day (Monday, Tuesday and so on)
ann1 = dict(font=dict(size=20),
            showarrow=False,
            text='Monday',
            # Specify text position (place text in a hole of pie)
            x=0.23,
            y=0.5
            )
ann2 = dict(font=dict(size=20),
            showarrow=False,
            text='Tuesday',
            # Specify text position (place text in a hole of pie)
            x=0.78,
            y=0.5
            )
# Specify layout to set the title and annotations to our traces
layout = go.Layout(title ='Pie chart subplots',
                   annotations=[ann1,ann2],
                   # Hide legend if you want
                   #showlegend=False
                   )
# Create fig with data and layout
fig = go.Figure(data=data,layout=layout)
# Plot the plot and save the file in your Python script directory
py.plot(fig, filename='subplot_pie_chart.html')

输出: Pie chart subplots 还建议您检查如何创建donut subplots并在两个轴上绘制子图 - link以便更好地理解。

关于python - ValueError : Invalid property specified for object of type plotly. graph_objs.Pie: 'xaxis',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52521194/

相关文章:

python - 如何在 Python 中获取组合 Unicode 字符串的 "visible"长度?

python - 无法弄清楚查询失败的原因

python - 如何在 Plotly 中为瀑布图添加总值列

python - 在 except 中引发异常而不调用原始异常

python - 从变量名或字符串创建列表或字典

python - 在 matplotlib 图上绘制裁剪后的背景图像

r - 向 scatter3d 图添加图例

Matlab如何填充箭袋箭头

R 中唯一的错误。default(x) : unique() applies only to vectors

python - 你能改变 plotly 中的子 plotly 标题位置吗?