登录
首页精彩阅读使用python来绘制漂亮的图表:出色的交互plotly篇!
使用python来绘制漂亮的图表:出色的交互plotly篇!
2020-05-27
收藏

我们最后来讲python另外一个非常出色的可视化工具,使用plotly创建出色的交互式图,最后,不再需要Matplotlib! 


Plotly具有三个重要功能:

· 悬停:将鼠标悬停在图表上时,将弹出注释

· 交互性:无需任何其他设置即可使图表互动(例如,穿越时空的旅程)

· 漂亮的地理空间图:Plotly具有一些内置的基本地图绘制功能,但是另外,可以使用mapbox集成来生成惊人的图表。

点图

我们通过运行fig = x。(PARAMS)然后调用fig.show()来调用绘图:

fig = px.scatter(
    data_frame=data[data['Year'] == 2018], 
    x="Log GDP per capita", 
    y="Life Ladder", 
    size="Gapminder Population", 
    color="Continent",
    hover_name="Country name",
    size_max=60
)
fig.show()

Plotly scatter plot, plotting Log GDP per capita against Life Ladder, where color indicates continent and size of the marker the population

散点图-漫步时光

fig = px.scatter(
    data_frame=data, 
    x="Log GDP per capita", 
    y="Life Ladder", 
    animation_frame="Year", 
    animation_group="Country name",
    size="Gapminder Population", 
    color="Continent", 
    hover_name="Country name", 
    facet_col="Continent",
    size_max=45,
    category_orders={'Year':list(range(2007,2019))}     
)
fig.show()

Visualization of how the plotted data changes over the years

并行类别-一种可视化类别的有趣方式

fig = px.bar(
    data, 
    x="Continent", 
    y="Gapminder Population", 
    color="Mean Log GDP per capita", 
    barmode="stack", 
    facet_col="Year",
    category_orders={"Year": range(2007,2019)},
    hover_name='Country name',
    hover_data=[
        "Mean Log GDP per capita",
        "Gapminder Population",
        "Life Ladder"
    ]
)
fig.show()

Seems like not all countries with high life expectations are happy!

条形图—交互式过滤器的示例

fig = px.bar(
    data, 
    x="Continent", 
    y="Gapminder Population", 
    color="Mean Log GDP per capita", 
    barmode="stack", 
    facet_col="Year",
    category_orders={"Year": range(2007,2019)},
    hover_name='Country name',
    hover_data=[
        "Mean Log GDP per capita",
        "Gapminder Population",
        "Life Ladder"
    ]
)
fig.show()

Filtering a bar chart is easy. Not surprisingly, South Korea is among the wealthy countries in Asia.

Choropleth plot-幸福如何随着时间而变化

fig = px.choropleth(
    data, 
    locations="ISO3", 
    color="Life Ladder", 
    hover_name="Country name", 
    animation_frame="Year")
fig.show()

Map visualization of how happiness evolves over the years. Syria and Afghanistan are at the very end of the Life Ladder range (unsurprisingly)


结束语

在本文中,我们学习了如何成为真正的Python可视化高手,了解了如何在快速探索方面提高效率,以及在再次召开董事会会议时如何创建更精美的图表。 还有交互式地图,这在绘制地理空间数据时特别有用哦。


本文翻译自Fabian Bosler的文章《Learn how to create beautiful and insightful charts with Python — the Quick, the Pretty, and the Awesome》 参考https://towardsdatascience.com/plotting-with-python-c2561b8c0f1f)

数据分析咨询请扫描二维码

最新资讯
更多
客服在线
立即咨询