国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

關于Python可視化Dash工具之plotly基本圖形示例詳解

瀏覽:133日期:2022-06-24 13:25:12

Plotly Express是對 Plotly.py 的高級封裝,內置了大量實用、現代的繪圖模板,用戶只需調用簡單的API函數,即可快速生成漂亮的互動圖表,可滿足90%以上的應用場景。

本文借助Plotly Express提供的幾個樣例庫進行散點圖、折線圖、餅圖、柱狀圖、氣泡圖、桑基圖、玫瑰環圖、堆積圖、二維面積圖、甘特圖等基本圖形的實現。

代碼示例

import plotly.express as pxdf = px.data.iris()#Index([’sepal_length’, ’sepal_width’, ’petal_length’, ’petal_width’, ’species’,’species_id’],dtype=’object’)# sepal_length sepal_width ... species species_id# 0 5.1 3.5 ... setosa 1# 1 4.9 3.0 ... setosa 1# 2 4.7 3.2 ... setosa 1# .. ... ... ... ... ...# 149 5.9 3.0 ... virginica 3# plotly.express.scatter(data_frame=None, x=None, y=None, # color=None, symbol=None, size=None,# hover_name=None, hover_data=None, custom_data=None, text=None,# facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None,# error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,# animation_frame=None, animation_group=None,# category_orders=None, labels=None, orientation=None,# color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, # range_color=None, color_continuous_midpoint=None,# symbol_sequence=None, symbol_map=None, opacity=None, # size_max=None, marginal_x=None, marginal_y=None,# trendline=None, trendline_color_override=None, # log_x=False, log_y=False, range_x=None, range_y=None,# render_mode=’auto’, title=None, template=None, width=None, height=None)# 以sepal_width,sepal_length制作標準散點圖fig = px.scatter(df, x='sepal_width', y='sepal_length')fig.show() #以鳶尾花類型-species作為不同顏色區分標志 colorfig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')fig.show() #追加petal_length作為散點大小,變位氣泡圖 sizefig = px.scatter(df, x='sepal_width', y='sepal_length', color='species',size=’petal_length’)fig.show() #追加petal_width作為額外列,在懸停工具提示中顯示為額外數據 hover_datafig = px.scatter(df, x='sepal_width', y='sepal_length', color='species', size=’petal_length’, hover_data=[’petal_width’])fig.show() #以鳶尾花類型-species區分散點的形狀 symbolfig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’])fig.show() #追加petal_width作為額外列,在懸停工具提示中以粗體顯示。 hover_namefig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’], hover_name='species')fig.show() #以鳶尾花類型編碼-species_id作為散點的文本值 textfig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’], hover_name='species', text='species_id')fig.show() #追加圖表標題 titlefig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’], hover_name='species', text='species_id',title='鳶尾花分類展示')fig.show() #以鳶尾花類型-species作為動畫播放模式 animation_framefig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’], hover_name='species', text='species_id',title='鳶尾花分類展示', animation_frame='species')fig.show() #固定X、Y最大值最小值范圍range_x,range_y,防止動畫播放時超出數值顯示fig = px.scatter(df, x='sepal_width', y='sepal_length', symbol='species' ,color='species', size=’petal_length’, hover_data=[’petal_width’], hover_name='species', text='species_id',title='鳶尾花分類展示', animation_frame='species',range_x=[1.5,4.5],range_y=[4,8.5])fig.show() df = px.data.gapminder().query('country==’China’')# Index([’country’, ’continent’, ’year’, ’lifeExp’, ’pop’, ’gdpPercap’, ’iso_alpha’, ’iso_num’],dtype=’object’)# country continent year ... gdpPercap iso_alpha iso_num# 288 China Asia 1952 ... 400.448611 CHN 156# 289 China Asia 1957 ... 575.987001 CHN 156# 290 China Asia 1962 ... 487.674018 CHN 156# plotly.express.line(data_frame=None, x=None, y=None, # line_group=None, color=None, line_dash=None,# hover_name=None, hover_data=None, custom_data=None, text=None,# facet_row=None, facet_col=None, facet_col_wrap=0, # facet_row_spacing=None, facet_col_spacing=None,# error_x=None, error_x_minus=None, error_y=None, error_y_minus=None,# animation_frame=None, animation_group=None,# category_orders=None, labels=None, orientation=None,# color_discrete_sequence=None, color_discrete_map=None,# line_dash_sequence=None, line_dash_map=None,# log_x=False, log_y=False,# range_x=None, range_y=None,# line_shape=None, render_mode=’auto’, title=None, # template=None, width=None, height=None)# 顯示中國的人均壽命fig = px.line(df, x='year', y='lifeExp', title=’中國人均壽命’)fig.show() # 以不同顏色顯示亞洲各國的人均壽命df = px.data.gapminder().query('continent == ’Asia’')fig = px.line(df, x='year', y='lifeExp', color='country',hover_name='country')fig.show() # line_group='country' 達到按國家去重的目的df = px.data.gapminder().query('continent != ’Asia’') # remove Asia for visibilityfig = px.line(df, x='year', y='lifeExp', color='continent', line_group='country', hover_name='country')fig.show() # bar圖df = px.data.gapminder().query('country == ’China’')fig = px.bar(df, x=’year’, y=’lifeExp’)fig.show() df = px.data.gapminder().query('continent == ’Asia’')fig = px.bar(df, x=’year’, y=’lifeExp’,color='country' )fig.show() df = px.data.gapminder().query('country == ’China’')fig = px.bar(df, x=’year’, y=’pop’, hover_data=[’lifeExp’, ’gdpPercap’], color=’lifeExp’, labels={’pop’:’population of China’}, height=400)fig.show() fig = px.bar(df, x=’year’, y=’pop’, hover_data=[’lifeExp’, ’gdpPercap’], color=’pop’, labels={’pop’:’population of China’}, height=400)fig.show() df = px.data.medals_long()# # nation medal count# # 0 South Korea gold 24# # 1 China gold 10# # 2 Canada gold 9# # 3 South Korea silver 13# # 4 China silver 15# # 5 Canada silver 12# # 6 South Korea bronze 11# # 7 China bronze 8# # 8 Canada bronze 12fig = px.bar(df, x='nation', y='count', color='medal', )fig.show() # 氣泡圖df = px.data.gapminder()# X軸以對數形式展現fig = px.scatter(df.query('year==2007'), x='gdpPercap', y='lifeExp', size='pop', color='continent',hover_name='country', log_x=True, size_max=60)fig.show() # X軸以標準形式展現fig = px.scatter(df.query('year==2007'), x='gdpPercap', y='lifeExp', size='pop', color='continent',hover_name='country', log_x=False, size_max=60)fig.show() # 餅狀圖px.data.gapminder().query('year == 2007').groupby(’continent’).count()# country year lifeExp pop gdpPercap iso_alpha iso_num# continent# Africa 52 52 52 52 52 52 52# Americas 25 25 25 25 25 25 25# Asia 33 33 33 33 33 33 33# Europe 30 30 30 30 30 30 30# Oceania 2 2 2 2 2 2 2df = px.data.gapminder().query('year == 2007').query('continent == ’Americas’')fig = px.pie(df, values=’pop’, names=’country’, title=’Population of European continent’)fig.show() df.loc[df[’pop’] < 10000000, ’country’] = ’Other countries’fig = px.pie(df, values=’pop’, names=’country’,title=’Population of European continent’, hover_name=’country’,labels=’country’)fig.update_traces(textposition=’inside’, textinfo=’percent+label’)fig.show() df.loc[df[’pop’] < 10000000, ’country’] = ’Other countries’fig = px.pie(df, values=’pop’, names=’country’,title=’Population of European continent’, hover_name=’country’,labels=’country’,color_discrete_sequence=px.colors.sequential.Blues)fig.update_traces(textposition=’inside’, textinfo=’percent+label’)fig.show() # 二維面積圖df = px.data.gapminder()fig = px.area(df, x='year', y='pop', color='continent',line_group='country')fig.show() fig = px.area(df, x='year', y='pop', color='continent',line_group='country', color_discrete_sequence=px.colors.sequential.Blues)fig.show() df = px.data.gapminder().query('year == 2007')fig = px.bar(df, x='pop', y='continent', orientation=’h’, hover_name=’country’, text=’country’,color=’continent’)fig.show() # 甘特圖import pandas as pddf = pd.DataFrame([ dict(Task='Job A', Start=’2009-01-01’, Finish=’2009-02-28’, Completion_pct=50, Resource='Alex'), dict(Task='Job B', Start=’2009-03-05’, Finish=’2009-04-15’, Completion_pct=25, Resource='Alex'), dict(Task='Job C', Start=’2009-02-20’, Finish=’2009-05-30’, Completion_pct=75, Resource='Max')])fig = px.timeline(df, x_start='Start', x_end='Finish', y='Task', color='Completion_pct')fig.update_yaxes(autorange='reversed')fig.show() fig = px.timeline(df, x_start='Start', x_end='Finish', y='Resource', color='Resource')fig.update_yaxes(autorange='reversed')fig.show() # 玫瑰環圖df = px.data.tips()# total_bill tip sex smoker day time size# 0 16.99 1.01 Female No Sun Dinner 2# 1 10.34 1.66 Male No Sun Dinner 3# 2 21.01 3.50 Male No Sun Dinner 3# 3 23.68 3.31 Male No Sun Dinner 2# 4 24.59 3.61 Female No Sun Dinner 4fig = px.sunburst(df, path=[’day’, ’time’, ’sex’], values=’total_bill’)fig.show() import numpy as npdf = 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() df = px.data.gapminder().query('year == 2007')fig = px.sunburst(df, path=[’continent’, ’country’], values=’pop’, color=’pop’, hover_data=[’iso_alpha’], color_continuous_scale=’RdBu’)fig.show() # treemap圖import numpy as npdf = px.data.gapminder().query('year == 2007')df['world'] = 'world' # in order to have a single root nodefig = px.treemap(df, path=[’world’, ’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() fig = px.treemap(df, path=[’world’, ’continent’, ’country’], values=’pop’, color=’pop’, hover_data=[’iso_alpha’], color_continuous_scale=’RdBu’, color_continuous_midpoint=np.average(df[’lifeExp’], weights=df[’pop’]))fig.show() fig = px.treemap(df, path=[’world’, ’continent’, ’country’], values=’pop’, color=’lifeExp’, hover_data=[’iso_alpha’], color_continuous_scale=’RdBu’)fig.show() fig = px.treemap(df, path=[ ’continent’, ’country’], values=’pop’, color=’lifeExp’, hover_data=[’iso_alpha’], color_continuous_scale=’RdBu’)fig.show() fig = px.treemap(df, path=[ ’country’], values=’pop’, color=’lifeExp’, hover_data=[’iso_alpha’], color_continuous_scale=’RdBu’)fig.show() # 桑基圖tips = px.data.tips()fig = px.parallel_categories(tips, color='size', color_continuous_scale=px.colors.sequential.Inferno)fig.show()

到此這篇關于關于Python可視化Dash工具之plotly基本圖形示例詳解的文章就介紹到這了,更多相關Python plotly基本圖形內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲在线看片 | 国产精选莉莉私人影院 | 精品一区二区在线观看 | 一级a毛片| 欧美一级二级三级视频 | 国产成人亚洲毛片 | 免费观看欧美一级毛片 | 国产色视频在线观看免费 | 久久综久久美利坚合众国 | 黄人成a动漫片免费网站 | 日本红怡院亚洲红怡院最新 | 成人免费夜片在线观看 | 久久综合婷婷 | 欧美精品 日韩 | 中文字幕一区二区三区精品 | 国产在线播放一区二区 | 国产精品视频网址 | 黄色成人在线网站 | 中文字幕在线观看不卡视频 | 日韩 欧美 国产 师生 制服 | 韩国三级大全久久网站 | 亚洲一区免费视频 | japanesetubesexfree| 美国一级视频 | 毛片免费观看久久欧美 | 美美女高清毛片视频黄的一免费 | 亚洲精品午夜久久久伊人 | 国产精品久久久久久福利漫画 | 免费特黄一级欧美大片在线看 | 亚洲黄色在线视频 | 精品乱人伦一区二区 | 亚洲视频在线观看 | 国产高清在线精品一区 | 爱啪网亚洲第一福利网站 | 欧美在线综合 | 亚洲国产成人久久99精品 | 久久精品在现线观看免费15 | 国产综合久久久久 | 一级全黄视频 | 午夜a毛片 | 日本久久久久久 |