Plotly equivalent for pd.DataFrame.hist

主宰稳场 提交于 2021-02-11 14:46:23

问题


I am looking for a way to imitate the hist method of pandas.DataFrame using plotly. Here's an example using the hist method:

import seaborn as sns
import matplotlib.pyplot as plt

# load example data set
iris = sns.load_dataset('iris')

# plot distributions of all continuous variables
iris.drop('species',inplace=True,axis=1)
iris.hist()
plt.tight_layout()

which produces:

How would one do this using plotly?


回答1:


Plotly has a histogram function built in so all you have to do is write

px.histogram()

and pass the data column and x=label, and it should work. Here's a link to the documentation https://plotly.com/python/histograms/




回答2:


You can make subplots using plotly's make_subplots() function. From there you add traces with the desired data and position within the subplot.

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=2, cols=2)

fig.add_trace(
    go.Histogram(x=iris['petal_length']),
    row=1, col=1
)
fig.add_trace(
    go.Histogram(x=iris['petal_width']),
    row=1, col=2
)
fig.add_trace(
    go.Histogram(x=iris['sepal_length']),
    row=2, col=1
)
fig.add_trace(
    go.Histogram(x=iris['sepal_width']),
    row=2, col=2
)

example



来源:https://stackoverflow.com/questions/64876044/plotly-equivalent-for-pd-dataframe-hist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!