前言:
此刻看官们对“python画柱状图”大概比较注意,我们都想要了解一些“python画柱状图”的相关文章。那么小编在网摘上网罗了一些有关“python画柱状图””的相关知识,希望小伙伴们能喜欢,姐妹们一起来了解一下吧!1 说明:
=====
1.1 资料来源:标点符
#感谢
1.2 知识点:python、matplotlib、csv和微软编辑器小技巧。
1.3 环境:
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。
2 city_populations.csv
===================
2.1 在线地址:
2.2 我喜欢离线的数据,那就下载下来。用360浏览器,打开上面的网页,复制地址,下载,新建下载,保存csv,即可。
如操作图:
2.3 打开数据看看,自动保存csv格式,不错吧。
3 用微软编辑器自带启动jupyter:
========================
3.1 新建一个11.py,小技巧:#%%就可以类似jupyter的一段段cell格式,并启动jupyter。
3.2 结果:
3.3 代码:
#注意代码:是带有#%%的格式,就是微软vscode自带jupyter#%%import pandas as pdimport matplotlib.pyplot as pltimport matplotlib.ticker as tickerimport matplotlib.animation as animationfrom IPython.display import HTML#%%df = pd.read_csv("data/city_populations.csv", usecols=['name', 'group', 'year', 'value'])df.head()# %%current_year = 2018dff = df[df['year'].eq(current_year)].sort_values(by='value', ascending=True).head(10)# %%fig, ax = plt.subplots(figsize=(15, 8))ax.barh(dff['name'], dff['value'])# %%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))ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']])for i, (value, name) in enumerate(zip(dff['value'], dff['name'])): ax.text(value, i, name, ha='right') # Tokyo: name ax.text(value, i-.25, group_lk[name], ha='right') # Asia: group name ax.text(value, i, value, ha='left') # 38194.2: valueax.text(1, 0.4, current_year, transform=ax.transAxes, size=46, ha='right')# %%fig, ax = plt.subplots(figsize=(15, 8))def draw_barchart(year): dff = df[df['year'].eq(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, name, size=14, weight=600, ha='right', va='bottom') ax.text(value-dx, i-.25, group_lk[name], size=10, color='#444444', ha='right', va='baseline') ax.text(value+dx, i, f'{value:,.0f}', size=14, ha='left', va='center') ax.text(1, 0.4, 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('top') 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.12, 'The most populous cities in the world from 1500 to 2018', transform=ax.transAxes, size=24, weight=600, ha='left') ax.text(1, 0, 'by @pratapvardhan; credit @jburnmurdoch', transform=ax.transAxes, ha='right', color='#777777', bbox=dict(facecolor='white', alpha=0.8, edgecolor='white')) plt.box(False) draw_barchart(2018)# %%import matplotlib.animation as animationfrom IPython.display import HTMLfig, ax = plt.subplots(figsize=(15, 8))animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1968, 2019))HTML(animator.to_jshtml()) # or use animator.to_html5_video() or animator.save()
3.4 保存动画mp4格式,由于不能保存gif格式,本文章需要gif格式,否则估计上传不了。
HTML(animator.to_jshtml()) # 备注一个,我是这么保存的,在上面的代码尾部进行修改,即可animator.save('/home/xgj/Desktop/datacsv/dd.mp4')
3.5 mp4格式转换gif格式:python代码
from moviepy.editor import *#指定路径clip=(VideoFileClip("/home/xgj/Desktop/datacsv/dd.mp4"))#指定路径clip.write_gif("/home/xgj/Desktop/datacsv/output.gif")#带有进度条的print("转换完成了")
3.6 output.gif
===自己整理并分享出来,喜欢微软编辑器的人可以这样使用jupyter===
喜欢的朋友,就请点赞、评论、转发、关注和收藏。
标签: #python画柱状图