在Python中使图表可视化的最佳方法 [英] Best way to make this chart visualization in Python

查看:61
本文介绍了在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!

解决方案

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:

这篇关于在Python中使图表可视化的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆