简体   繁体   English

Matplotlib Barh 过渡平滑

[英]Matplotlib Barh transitions smooth

I'm a noob when it comes to Matplotlib and visualization in general.总的来说,当涉及到 Matplotlib 和可视化时,我是个菜鸟。 I've been following this tutorial: https://thecleverprogrammer.com/2020/06/23/bar-chart-race-tutorial-in-python-with-matplotlib/我一直在关注本教程: https : //thecleverprogrammer.com/2020/06/23/bar-chart-race-tutorial-in-python-with-matplotlib/

Which provides a good base for solving my problems my only issue is the transitions are very abrupt - how would I go about adding smooth transitions like this - this is an example of how the transitions should look don't mind the actual data:这为解决我的问题提供了一个很好的基础,我唯一的问题是过渡非常突然- 我将如何添加这样的平滑过渡 - 这是过渡看起来如何不介意实际数据的示例: 文本

As far as I'm aware - the animation function produces a frame for every interval - how can I get frames where the bars are animating over each other?据我所知 - 动画功能为每个间隔生成一个帧 - 我怎样才能获得条形相互动画的帧?

Edit:编辑:

Here's the code I'm looking at这是我正在查看的代码

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage

url = 'https://gist.githubusercontent.com/johnburnmurdoch/4199dbe55095c3e13de8d5b2e5e5307a/raw/fa018b25c24b7b5f47fd0568937ff6c04e384786/city_populations'

df = pd.read_csv(url, usecols=['name', 'group', 'year', 'value'])
df.head(3)

colors = dict(zip(
    ["India", "Europe", "Asia", "Latin America", "Middle East", "North America", "Africa"],
    ["#adb0ff", "#ffb3ff", "#90d595", "#e48381", "#aafbff", "#f7bb5f", "#eafb50"]
))
group_lk = df.set_index('name')['group'].to_dict()

fig, ax = plt.subplots(figsize=(15, 8))


def draw_barchart(current_year):
    dff = df[df['year'].eq(current_year)].sort_values(by='value', ascending=True).tail(10)
    ax.clear()
    ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']])
    dx = dff['value'].max() / 200
    for i, (value, name) in enumerate(zip(dff['value'], dff['name'])):
        ax.text(value-dx, i,     "   {} ({})".format(name,round(value, 2)),           size=12, color='#777777' ,weight=600, ha='left', va='center')

    ax.text(1, 0.4, current_year, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800)
    #ax.text(0, 1.06, 'Population (thousands)', transform=ax.transAxes, size=12, color='#777777')
    ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    ax.xaxis.set_ticks_position('bottom')
    ax.tick_params(axis='x', colors='#777777', labelsize=12)
    ax.set_yticks([])
    ax.margins(0, 0.01)
    ax.grid(which='major', axis='x', linestyle='-')
    ax.set_axisbelow(True)
    ax.text(0, 1.15, 'This is a title',
            transform=ax.transAxes, size=24, weight=600, ha='left', va='top', color="#777")
    ax.text(1, 0, "My description here", transform=ax.transAxes, color='#777777', ha='right',
            bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
    plt.box(False)
    plt.subplots_adjust(left=0.15, right=0.80, bottom=0.20, top=0.86)
    


fig, ax = plt.subplots(figsize=(15, 8))
plt.show()
animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1900, 2019))
animator.save('linex.gif', dpi=180, writer='imagemagick')

As mentioned in the comments, matplotlib animations do not allow for smooth transitions.正如评论中提到的,matplotlib 动画不允许平滑过渡。 It's because the graph is created and played back frame by frame.这是因为图形是逐帧创建和播放的。 It doesn't create a graph to complement between frames.它不会创建一个图表来在帧之间进行补充。 If you want to use python for bar chart tracing, there are some good dedicated libraries in python.如果您想使用 python 进行条形图跟踪,python 中有一些很好的专用库。 The name of it is bar_char_race .它的名字是bar_char_race You can also adjust the details.您还可以调整细节。 Depending on the file format, you may need to pre-install ffmpeg or other software.根据文件格式,您可能需要预先安装 ffmpeg 或其他软件。

import pandas as pd
import bar_chart_race as bcr

url = 'https://gist.githubusercontent.com/johnburnmurdoch/4199dbe55095c3e13de8d5b2e5e5307a/raw/fa018b25c24b7b5f47fd0568937ff6c04e384786/city_populations'

df = pd.read_csv(url, usecols=['name', 'group', 'year', 'value'])

df.value = df.value.astype(int)
# top 10 city
dfs = df.groupby(['group','name'],as_index=False)['value'].max().sort_values(by='value', ascending=False).head(10)
top10 = dfs.name[:10].values

dff = df.query('year >= 1900 and name in @top10')
dff = dff.pivot_table(index='year',columns='name', values='value')
dff.fillna(0, axis=1, inplace=True)
dff.index = pd.to_datetime(dff.index, format='%Y')

bcr.bar_chart_race(df=dff, n_bars=10, filename='poptop10.mp4', period_fmt='%Y')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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