Код: Выделить всё
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame({"college_name":list("ABCD"),"in-state_tuition":np.random.randint(5000,7000,4),"out-of-state_tuition":np.random.randint(8000,15000,4)})
px.bar(
data_frame = df,
x = "college_name",
y = ["in-state_tuition","out-of-state_tuition"],
opacity = 0.9,
orientation = "v",
barmode = 'group',
title='Annual In-State Tuition vs Out-of-state Tuition',
).show()
Я получил пример: />https://plotly.com/python/bar-charts/
Печать рис.
print(fig)
print(fig.data[0].y)
print(fig.data[1].y)
Figure({
'data': [{'alignmentgroup': 'True',
'hovertemplate': 'variable=in-state_tuition
college_name=%{x}
value=%{y}',
'legendgroup': 'in-state_tuition',
'marker': {'color': '#636efa', 'opacity': 0.9, 'pattern': {'shape': ''}},
'name': 'in-state_tuition',
'offsetgroup': 'in-state_tuition',
'orientation': 'v',
'showlegend': True,
'textposition': 'auto',
'type': 'bar',
'x': array(['A', 'B', 'C', 'D'], dtype=object),
'xaxis': 'x',
'y': {'bdata': 'TBvhF68atRc=', 'dtype': 'i2'},
'yaxis': 'y'},
{'alignmentgroup': 'True',
'hovertemplate': 'variable=out-of-state_tuition
college_name=%{x}
value=%{y}',
'legendgroup': 'out-of-state_tuition',
'marker': {'color': '#EF553B', 'opacity': 0.9, 'pattern': {'shape': ''}},
'name': 'out-of-state_tuition',
'offsetgroup': 'out-of-state_tuition',
'orientation': 'v',
'showlegend': True,
'textposition': 'auto',
'type': 'bar',
'x': array(['A', 'B', 'C', 'D'], dtype=object),
'xaxis': 'x',
'y': {'bdata': 'XSmbMYYk/B8=', 'dtype': 'i2'},
'yaxis': 'y'}],
'layout': {'barmode': 'group',
'legend': {'title': {'text': 'variable'}, 'tracegroupgap': 0},
'template': '...',
'title': {'text': 'Annual In-State Tuition vs Out-of-State Tuition'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'college_name'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'value'}}}
})
[6988 6113 6831 6069]
[10589 12699 9350 8188]
< /code>
Я использую следующие версии:
plotly: 6.1.2
numpy: 1.23.5 < /p>
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-properly