DataScienceNovice
DataScienceNovice

Reputation: 502

Best way to make this chart visualization in Python

I apologize if this is an inappropriate question - I'm new to this site.

I've got a line chart (which can be found here), and I want to be able to programmatically make it in Python.

As you can see, it's a line graph without an incremented x-axis. There's boxes on the x-axis, each with a descriptor and a number. The point that is plotted is the aforementioned number associated with each column.

I'm familiar with the visualization library Bokeh, but couldn't seem to come up with a good result. Matplotlib is also an option, but that library usually generates fairly crude graphics. Could anyone tell me if they've seen this kind of chart before (like, a more specific name than "line graph")? My only real restriction is that I need this to be fairly easy to render on the web, hence why I initially looked at Bokeh. Any suggestions welcome!

Upvotes: 3

Views: 730

Answers (1)

Derek O
Derek O

Reputation: 19610

If I understand your problem correctly, you want a line chart whose x-axis only has minor ticks labeled.

I agree that matplotlib looks crude: I think Plotly is a pretty friendly library for making graphics that are interactive, and render nicely on a webpage.

To embed the plot on a webpage, you can use the chart_studio library, then create a Chart Studio account with a username and get your API key.

import plotly as py
import plotly.graph_objects as go 
import chart_studio
import chart_studio.plotly as ch_py
import chart_studio.tools as tls

x_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y_data = [3, 5, 3.3, 6.2, 3.5, 4.2, 3.7, 6.3, 4.4]

fig = go.Figure(data = go.Scatter(x = x_data, y = y_data))

fig.update_layout(
    xaxis = dict(
        tickmode = 'array',
        tickvals = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5],
        ticktext = ['str1', 'str2', 'str3', 'str4', 'str5', 'str6', 'str7', 'str8', 'str9'] 
    ),
    yaxis = dict(
        tickmode = 'array',
        tickvals = [1,2,3,4,5,6,7]
    )
)

fig.show()

# create a chartstudio account and generate an API key
# username = 'xxxxxxxx'
# api_key = 'xxxxxxxxxx'

# chart_studio.tools.set_credentials_file(username=username, api_key=api_key)
# ch_py.plot(fig, filename = 'my_filename', auto_open = True)

It will render in the browser automatically when you run it:

enter image description here

Upvotes: 2

Related Questions