0
votes

Well, I am trying to plot a Bar Graph in Plotly where I need to show 3 years of data in a grouped bar chart though the chart is displaying the data in the chart not showing data correctly all the bars are equal in the graph Something like this: enter image description here

Here is my code for plotting:

data=[go.Bar(x=nasdaq['Sector'],y=recent_ipos['IPO Year'],textangle=-45,name='2015'),
go.Bar(x=nasdaq['Sector'],y=recent_ipos['IPO Year'],textangle=-45,name='2016'),
go.Bar(x=nasdaq['Sector'],y=recent_ipos['IPO Year'],textangle=-45,name='2017')
]

layout=go.Layout(title="NASDAQ Market Capitalization IPO yEAR (million USD)",barmode='group')
fig=go.Figure(data=data,layout=layout)
fig.show(renderer="colab")

Here is my code which I am using to extract the data for 3 years:

recent_ipos = nasdaq[nasdaq['IPO Year'] > 2014]
recent_ipos['IPO Year'] = recent_ipos['IPO Year'].astype(int)

I tried to extract the 2015 data here using an array but I don't find an appropriate method here to extract an element from the array

ipo2015=np.array(recent_ipos['IPO Year'])
ipo2015

I am not sure if this is the right way to extract the particular year data or not?? Things I want to know here are :

How to extract year data appropriately in the graph using Plotly?

What changes I should make to solve this inconsistency in the graph?

What should I put in Y= in all the three groups bars??

How to extract the years dynamically rather than manually?

Hope to receive help from this amazing community on StackOverflow. Thanks in advance.!!

1

1 Answers

1
votes

I wrote the code under the assumption that the data on which the question is based is in data frame format. The data is taken from plotly. The query() can also be used as a variable using @ as shown in the code.

import plotly.graph_objects as go
import plotly.express as px

# yyyy = [1992,1997,2002]
df = px.data.gapminder()
continent = df['continent'].unique().tolist()
yyyy = df['year'].unique().tolist()[-3:] # update
data = []
for y in yyyy:
    # tmp_df = df.query('year == @y')
    tmp_df = df[df['year'] == y].groupby('continent')['pop'].sum()
    data.append(go.Bar(x=tmp_df.index, y=tmp_df, name=y))
    
# Change the bar mode
fig = go.Figure(data)
fig.update_layout(barmode='group')
fig.show()

enter image description here