In [ ]:
# 旭日图
# 通过清晰展示层次数据,超越了传统的饼图和环图。它使用同心圆,每个圆代表层次中的一级。中心是根,扇形表示节点。
# 每个扇形的大小反映了它的值,直观地理解数据的重要性。在可视化文件系统层次结构、用户导航路径、市场细分和遗传数据方面非常有用。
# 以下是一个使用 Plotly 库创建旭日图的示例。
In [6]:
import plotly.express as px
import numpy as np
import pandas as pd
# 加载 2007 年的 gapminder 数据
df = px.data.gapminder().query("year == 2007")
# 创建旭日图
fig = px.sunburst(df,
path=['continent', 'country'],
values='pop',
color='lifeExp',
hover_data=['iso_alpha'],
color_continuous_scale='RdBu',
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
# 显示图形
fig.show()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[6], line 9 6 df = px.data.gapminder().query("year == 2007") 8 # 创建旭日图 ----> 9 fig = px.sunburst(df, 10 path=['continent', 'country'], 11 values='pop', 12 color='lifeExp', 13 hover_data=['iso_alpha'], 14 color_continuous_scale='RdBu', 15 color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop'])) 17 # 显示图形 18 fig.show() File D:\Python310\lib\site-packages\plotly\express\_chart_types.py:1517, in sunburst(data_frame, names, values, parents, path, ids, color, color_continuous_scale, range_color, color_continuous_midpoint, color_discrete_sequence, color_discrete_map, hover_name, hover_data, custom_data, labels, title, template, width, height, branchvalues, maxdepth) 1515 if path is not None and branchvalues is None: 1516 branchvalues = "total" -> 1517 return make_figure( 1518 args=locals(), 1519 constructor=go.Sunburst, 1520 trace_patch=dict(branchvalues=branchvalues, maxdepth=maxdepth), 1521 layout_patch=layout_patch, 1522 ) File D:\Python310\lib\site-packages\plotly\express\_core.py:1935, in make_figure(args, constructor, trace_patch, layout_patch) 1933 args = build_dataframe(args, constructor) 1934 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None: -> 1935 args = process_dataframe_hierarchy(args) 1936 if constructor == "timeline": 1937 constructor = go.Bar File D:\Python310\lib\site-packages\plotly\express\_core.py:1637, in process_dataframe_hierarchy(args) 1635 if cols: 1636 df_tree[cols] = dfg[cols] -> 1637 df_all_trees = df_all_trees.append(df_tree, ignore_index=True) 1639 # we want to make sure than (?) is the first color of the sequence 1640 if args["color"] and discrete_color: File D:\Python310\lib\site-packages\pandas\core\generic.py:6299, in NDFrame.__getattr__(self, name) 6292 if ( 6293 name not in self._internal_names_set 6294 and name not in self._metadata 6295 and name not in self._accessors 6296 and self._info_axis._can_hold_identifiers_and_holds_name(name) 6297 ): 6298 return self[name] -> 6299 return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'append'
In [ ]: