在Python中使用 Plotly 的条形图
Plotly是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
Plotly 中的条形图
带有plotly express的条形图,对于plotly来说是非常方便和高级的前沿,有利于数据的多样化操作,有利于生成易于样式化的图形。用 px。 bar,DataFrame中的每一行都表示为一个矩形标记,可以是水平的,也可以是垂直的。 Alls 有助于显示离散类别之间的比较。图表中的一个轴显示正在比较的特定类别,另一个轴表示测量值。它可以使用 plotly.express 的 bar() 方法创建。
Syntax: plotly.express.bar(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, hover_name=None, hover_data=None, custom_data=None, text=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, animation_frame=None, animation_group=None, category_orders={}, labels={}, color_discrete_sequence=None, color_discrete_map={}, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, opacity=None, orientation=None, barmode=’relative’, log_x=False, log_y=False, range_x=None, range_y=None, title=None, template=None, width=None, height=None)
Parameters:
data_frame: DataFrame or array-like or dict needs to be passed for column names.
x: Either a name of a column in data_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.
y: Either a name of a column in data_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.
color: Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign color to marks.
hover_data: Either a list of names of columns in data_frame, or pandas Series, or array_like objects or a dict with column names as keys, with values True
示例 1:
Python3
import plotly.express as px
import numpy
# creating random data through randomint
# function of numpy.random
np.random.seed(42)
random_x= np.random.randint(1,101,100)
random_y= np.random.randint(1,101,100)
fig = px.bar(random_x, random_y)
fig.show()
Python3
import plotly.express as px
# Loading the iris dataset
df = px.data.iris()
fig = px.bar(df, x="sepal_width", y="sepal_length")
fig.show()
输出:
示例 2:使用 iris 数据集
Python3
import plotly.express as px
# Loading the iris dataset
df = px.data.iris()
fig = px.bar(df, x="sepal_width", y="sepal_length")
fig.show()
输出: