blog

plotly-express-11-plotly は棒グラフを実装している

px.bar を用いると,DataFrame の各行がマークとして扱われます. 複数の行が同じ x の値を共有する場合(ここでは Female または Ma......

Jan 7, 2021 · 19 min. read
シェア

plotly-express-11-plotly棒グラフの実装

この記事では、plotlyで棒グラフBarを描く方法を、主に2つの方法で説明します:

  • ピクセル単位の図の高さ..bar
  • ピクセル単位の図の高さ..Bar

good method

系列型データのインデックスと値を異なる色の列で表示する良い方法です。

図形の

テキスト表示部

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
# 颜色的随机生成:# # 加上6位数字构成def random_color_generator(number_of_colors): color = ["#"+''.join([random.choice('ABCDEF') for j in range(6)]) for i in range(number_of_colors)]return colortrace = go.Bar(x =t_srs.index,y =t_srs.values,text = text,marker = dict( color = random_color_generator(100), line = dict(color='rgb(8, )', # 列の外周線の色と幅.width = 1.5)),opacity = 0.7 # 透明度设置)# 数据部分:一定是列表的形式data = [trace]# 布局设置layout = go.Layout( title = 'Prime genre', # 図全体のタイトルmargin = dict(l = 100 # 左边距离),xaxis = dict( title = 'Type of app' # 2図形のタイトル(ピクセル単位).),yaxis = dict(title = 'Count of app'),width = 900, # figure的宽高height = 005)fig = go.Figure(data=data, layout=layout)fig.update_traces(textposition="outside")fig.show()

int (デフォルトは None) - ピクセル単位の図の高さ.

無作為化モジュールの導入により、結果は実行ごとに異なります。

ライブラリのインポート

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
import pandas as pdimport numpy as npimport plotly_express as pximport plotly.graph_objects as goimport dashimport dash_core_components as dccimport dash_html_components as html

ピクセル単位の図の高さ..bar

px.barを使用すると、DataFrameの各行が矩形のマークとして表現されます。

basic

  • ounter(line
  • ounter(line
  • ounter(line
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x="year", y="pop")fig.show()

ピクセル単位の図の高さ.

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
fig = px.bar(data_canada, x="year",y="pop",
 color="lifeExp",height=400, hover_data=["lifeExp","gdpPercap"], labels={"pop":"population of Canada"} # y 軸の名前を pop に変更する。)fig.show()

積み上げ棒グラフ

複数の行が同じ x の値を共有する場合、デフォルトでは矩形は互いに重ねられます。

スタッキングディスプレイ

グループ分けによるプレゼンテーション

facetted subplots

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
fig = px.bar(df, x="sex",y="total_bill",
 color="smoker",barmode="group", facet_col="day", # in the horizontal direction 水平 facet_row="time", # in the vertical direction int (デフォルトは None) - ピクセル単位の図の高さ. category_orders={"day":["Thur","Fri","Sat","Sun"], # 2int (デフォルトは None) - ピクセル単位の図の高さ. "time":["Lunch","Dinner"]})fig.show()

ピクセル単位の図の高さ..Bar

basic

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
animals = ["cat","dog","pig","chicken","monkeys"]
values = [,60,30]fig = go.Figure(data=(go.Bar(x=animals,y=values)))fig.show()

grouped bar chart

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
animals = ["cat","dog","pig","chicken","monkeys"]
values_1 = [,60,40]values_2 = [,50,20]fig = go.Figure(data=[ go.Bar(x=animals,y=values_1,name="shenzhen Zoo"), # int (デフォルトは None) - ピクセル単位の図の高さ. go.Bar(x=animals,y=values_2,name="Tokyo Zoo"),])# change the bar mode棒グラフのモードを更新するfig.update_layout(barmode="group") # グループ化の形式.fig.show()

stack bar chart

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
animals = ["cat","dog","pig","chicken","monkeys"]
values_1 = [,60,40]values_2 = [,50,20]fig = go.Figure(data=[ go.Bar(x=animals,y=values_1,name="shenzhen Zoo"), # int (デフォルトは None) - ピクセル単位の図の高さ. go.Bar(x=animals,y=values_2,name="Tokyo Zoo"),])# change the bar mode棒グラフのモードを更新するfig.update_layout(barmode="stack") # int (デフォルトは None) - ピクセル単位の図の高さ.fig.show()

Bar Chart with Hover Text

  • ホバーテキストメッセージ表示
  • x軸ラベルの回転
  • テキストメッセージの表示とY軸上の位置表示
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
x = ['Product A', 'Product B', 'Product C']
y = []# Use the hovertext kw argument for hover textfig = go.Figure(data=[go.Bar(x=x, y=y, text=y,textposition="auto", # 矩形枠内に表示されるテキスト text とテキスト位置 textposition hovertext=['27% market share', '24% market share', '19% market share']) # ホヴァーしてテキストを表示する])# update_tracesfig.update_traces(marker_color='rgb(1,)', marker_line_color='rgb()', # int (デフォルトは None) - ピクセル単位の図の高さ. marker_line_width=1.5, # 矩形の枠の線の太さ.opacity=0.8)# 图表标题fig.update_layout(title_text='January 2013 Sales Report',xaxis_tickangle=-45) #  + x軸ラベルは,45°回転されるfig.show()

Controlling text fontsize with uniformtext

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, x="country", y="pop", text="pop")fig.update_traces(texttemplate="%{text:.2s}", # 有効数字2桁を保持する textposition="outside") # 図が外側に表示されるfig.update_layout(uniformtext_minsize=10, uniformtext_mode="hide")fig.show()

Customizing Individual Bar Widths

矩形ボックスの幅をカスタマイズする方法

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
fig = go.Figure(data=[go.Bar(
x=[1, 2, 3, 5.5, 10],y=[, 4, 2], width=[0.8, 0.8, 0.8, 3.5, 4] # 各矩形ボックスの幅を指定する)])fig.show()

Bar Chart with Relative Barmode

相対 "バーモードでは、バーは互いに重なり、負の値は軸の下、正の値は軸の上となります。軸の下に負の値、上に正の値。

barmodeパラメータの相対値で設定

Bar Chart with Sorted or Ordered Categories

"category descending" categoryorderには、カテゴリー名の英数字順には「category ascending」、数値順には「total ascending」または「total descending」を設定します。詳細については、 categoryorder 参照してください。

特定のトレースでバーをソートすることはできません。もちろん、もっとカスタマイズが必要であれば、プロットするときにいつでもソートすることができます。

ピクセル単位の図の高さ.

  • data_frame ( ) - int (デフォルト None)) ピクセル単位の図の高さ。. Array-like and dict are tranformed internally to a pandas DataFrame. Optional: if missing, a DataFrame gets constructed under the hood using the other arguments.
  • x ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the x axis in cartesian coordinates. Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were long' ではなく 'wide' です..
  • y ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to position marks along the y axis in cartesian coordinates. Either x or y can optionally be a list of column references or array_likes, in which case the data will be treated as if it were long' ではなく 'wide' です..
  • color ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign color to marks.
  • facet_row ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign marks to facetted subplots in the vertical direction.
  • facet_col ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign marks to facetted subplots in the horizontal direction.
  • facet_col_wrap () - ファセット列の最大数.. Wraps the column variable at this width, so that the column facets span multiple rows. Ignored if 0, and forced to 0 if facet_row or a marginal is set.
  • hover_name ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like appear in bold in the hover tooltip.
  • hover_data ( *, or* ) - データの列名のリスト._frame, or pandas Series, or array_like objects or a dict with column names as keys, with values True False , or a formatting string, for example :.3fheight (int (デフォルト None)) - ピクセル単位の図の高さ.%aint (デフォルトは None) - ピクセル単位の図の高さ., and list-like data to appear in hover as second element Values from these columns appear as extra data in the hover tooltip.
  • custom_data ( ) - データ内の列の名前_frame, or pandas Series, or array_like objects Values from these columns are extra data, to be used in widgets or Dash callbacks for example. This data is not user-visible but is included in events emitted by the figure
  • text ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like appear in the figure as text labels.
  • error_x ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to size x-axis error bars. If error_x_minus is None, error bars will be symmetrical, otherwise error_x is used for the positive direction only.
  • error_x_minus ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to size x-axis error bars in the negative direction. Ignored if error_x is None.
  • error_y ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to size y-axis error bars. If error_y_minus is None, error bars will be symmetrical, otherwise error_y is used for the positive direction only.
  • error_y_minus ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to size y-axis error bars in the negative direction. Ignored if error_y is None.
  • animation_frame ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign marks to animation frames.
  • animation_group ( ) - データの列名._frame, or a pandas Series or array_like object. Values from this column or array_like are used to provide object-constancy across animation frames: rows with matching [](https://./--//...#)_ups will be treated as if they describe the same object in each frame.
  • category_orders (dict with str keys and list of str values (default {})) - デフォルトでは, in Python 3.6+, the order of categorical values in axes, legends and facets depends on the order in which these values are first encountered in data_frame . This parameter is used to force a specific ordering of values per column. The keys of this dict should correspond to column names, and the values should be lists of strings corresponding to the specific display order desired.
  • labels (dict with str keys and str values (default {})) - デフォルトでは, column names are used in the figure for axis titles, legend entries and hovers. This parameter allows this to be overridden. The keys of this dict should correspond to column names, and the values should correspond to the desired label to be displayed.
  • color_discrete_sequence () - 文字列は,有効な CSS-colors を定義する必要があります. When color is set and the values in the corresponding column are not numeric, values in that column are assigned colors by cycling through color_discrete_sequence in the order described in category_orders, unless the value of color is a key in color_discrete_map. Various useful color sequences are available in the plotly.express.colors submodules, specifically plotly.express.colors.qualitative.
  • color_discrete_map (dict with str keys and str values (default {})) - 文字列値は、 有効な CSS-colors を定義する必要があ り ます。color_discrete_sequence to assign a specific colors to marks corresponding with specific values. Keys in color_discrete_map should be values in the column denoted by color. Alternatively, if the values of color are valid colors, the string 'identity' may be passed to cause them to be used directly.
  • color_continuous_scale () - 文字列は、 有効な CSS-colors を定義する必要があ り ます。色で表 さ れる列が数値デー タ を含んでい る と き に、 連続的な色スケールを構築する ために用い ら れます。. Various useful color scales are available in the plotly.express.colors submodules, specifically plotly.express.colors.sequential, plotly.express.colors.diverging and plotly.express.colors.cyclical.
  • range_color () - 指定された場合, overrides auto-scaling on the continuous color scale.
  • color_continuous_midpoint (number (default None)) - 設定されている場合, computes the bounds of the continuous color scale to have the desired midpoint. Setting this value is recommended when using plotly.express.colors.diverging color scales as the inputs to color_continuous_scale.
  • opacity () - 0 から 1 の間の値.. Sets the opacity for markers.
  • orientation - x(y)がカテゴリカルで y(x)が連続の場合。
  • barmode (str (default 'relative')) - 以下のいずれか'group', 'overlay' or 'relative' In 'relative' mode, bars are stacked above zero for positive values and below zero for negative values. In 'overlay' mode, bars are drawn on top of one another. In 'group' mode, bars are placed beside each other.
  • log_x (boolean (default False)) - 真の場合, the x-axis is log-scaled in cartesian coordinates.
  • log_y (boolean (default False)) - 真の場合, the y-axis is log-scaled in cartesian coordinates.
  • range_x () - 指定された場合, overrides auto-scaling on the x-axis in cartesian coordinates.
  • range_y () - 指定された場合, overrides auto-scaling on the y-axis in cartesian coordinates.
  • title () - 図形のタイトル.
  • template ( ) - 図テンプレートの名前,あるいは定義..
  • width (int (default None)) - int (デフォルトは None) - ピクセル単位の図幅..
  • height (int (default None)) - ピクセル単位の図の高さ..
Read next

JSコア理論 Reactの基本概念と仮想DOM

文字列でもHTMLでもないJSXは、本質的にはReact要素と呼ばれる()を介して作成されるオブジェクトの構文拡張であり、それに近いものです。 ReactはJSXの使用を強制するのではなく、マークアップとロジックを一緒にして、フォーカスの共有を可能にするコンポーネントを形成します。

Jan 7, 2021 · 13 min read