简体   繁体   中英

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.

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]
                ),
        )

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM