0

I am plotting bar chart using below code as an example:

fig = make_subplots(rows=1, cols=2, shared_yaxes=True, horizontal_spacing= 0)

y = ['10', '20', '30', '40', '50','60']
width=2.9

fig.add_trace(go.Bar(x=[34, 64, 20,24,12,89], y=y,orientation='h',name = '1',marker_color='gold', width=width),row=1, col=1)
fig.add_trace(go.Bar(x=[14, 24, 50,34,9,104], y=y,orientation='h',name = '2',marker_color='darkorange',width=width),row=1, col=1)
fig['layout']['xaxis']['autorange'] = "reversed"
fig.add_trace(go.Bar(x=[17,46,68,22,12,93], y=y,orientation='h',name = '3',marker_color='deepskyblue',width=width),row=1, col=2)
fig.add_trace(go.Bar(x=[57,45,14,44,8,100], y=y,orientation='h',name = '4',marker_color='royalblue',width=width),row=1, col=2)
fig.update_layout(title_text="Data Chart",title_x=0.45, bargap=0.4)
fig.show()

but when i am trying plot the same chart using pandas dataframe ,I have with me, I am getting error and chart is not same as i have like above. Datagrame I have is like

       1     2      3      4
100   23    34     56     67
110   46    78     94     56
120   71    88     17     85
130   92    99     72     35
140   39    35     64     72
150   81    50    120     12

Is there a easy fix so that I can achieve exact bar chart as I have in image above using Pandas Dataframe.

1 Answer 1

1

Identical to the sample script by specifying a column of data frames, respectively

import pandas as pd
import numpy as np
import io

data = '''
       1     2      3      4
100   23    34     56     67
110   46    78     94     56
120   71    88     17     85
130   92    99     72     35
140   39    35     64     72
150   81    50    120     12
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

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

fig = make_subplots(rows=1, cols=2, shared_yaxes=True, horizontal_spacing= 0)

Y = df.index
width=2.9

fig.add_trace(go.Bar(x=df['1'], y=y,orientation='h',name='1',marker_color='gold', width=width),row=1, col=1)
fig.add_trace(go.Bar(x=df['2'], y=y,orientation='h',name='2',marker_color='darkorange',width=width),row=1, col=1)
fig['layout']['xaxis']['autorange'] = "reversed"
fig.add_trace(go.Bar(x=df['3'], y=y,orientation='h',name='3',marker_color='deepskyblue',width=width),row=1, col=2)
fig.add_trace(go.Bar(x=df['4'], y=y,orientation='h',name='4',marker_color='royalblue',width=width),row=1, col=2)
fig.update_layout(title_text="Data Chart",title_x=0.45, bargap=0.4)
fig.show()

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much for your help. I have few more question if you can please have a look in my profile to help me fix it.
I looked at the profiles. Each one has an answer but it is not accepted. Which question do you want help with? I will help in any way I can.
hi when ploting chart with different dataset my y axis tick for 30000 show 30k and x axis shows 1M for 1,000,000. tried all solution from internet but nothing is working. Do you have any fix for this.
fig.update_yaxes(exponentformat='none')I haven't checked in hand, but I think it's possible in this format. See this page.
hi one more issue i have y axis is not showing all ticks,like i have 100,110,120 but it shows like 100,120,130.any fix please. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.