Trung Vo
Trung Vo

Reputation: 55

how to remove gaps in bar-chart using plotly in python

I am trying to use plotly in python to create a bar-chart, x and y values as below

x_val = [1,3,4,7,10]
y_val = [1,2,3,4,5] 

Here is the script I used to create a plot

from plotly.offline import plot
import plotly.graph_objs as go

trace = go.Bar(x=x_val, y=y_val)
layout = go.Layout(autosize=True)
fig = go.Figure(data=[trace], layout=layout)
plot(fig)

As the x-axis does not have the values of 2,5,6,8,9, thus there are gaps in a bar chart. I do not want to have a gap between them, however converting all x-values to string/object does not help at all. Would you please advise how to resolve this issue? Thank you.

Upvotes: 3

Views: 2532

Answers (1)

Sheldore
Sheldore

Reputation: 39052

Here is a solution: Just plot only the y-values and set the x-ticks manually.

trace = go.Bar(y=y_val)

layout = go.Layout(autosize=True)
layout=dict(
                xaxis=dict(
                tickvals=[i for i in range(len(x_val))],
                ticktext=[x for x in x_val]
                ),
        )

enter image description here

Upvotes: 2

Related Questions