8000字 | Python数据可视化,完整版实操指南 !
简说Python
共 11240字,需浏览 23分钟
· 2021-05-09
点击“简说Python”,选择“置顶/星标公众号”
福利干货,第一时间送达
1. 前言
![](https://filescdn.proginn.com/6cafd8124b632ea8585bf92728cc23fe/c7d79eb04996c26780de5c52593bdecd.webp)
2. pandas
import pandas as pd
df = pd.read_csv('temporal.csv')
df.head(10) #View first 10 data rows
![](https://filescdn.proginn.com/bdd8e8357377035fa795a5d8838a6e15/b33bf94f139f7c02f8a7b2d2b1f62f75.webp)
df.describe()
![](https://filescdn.proginn.com/239a0b79683cf9f6904a4bda0a91bdf2/edbf361cb73cb8f80d3f7a05d1c09162.webp)
df.info()
![](https://filescdn.proginn.com/6dbacbbad3eb608cef3c42280cd6ab01/55eccb78129967e4ebc399bd4b47c2ff.webp)
![](https://filescdn.proginn.com/1fe3b90f2893256f6d265d831b3016e4/64de10698fbde7ebd9edf4a687298a4d.webp)
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)
format_dict = {'data science':'${0:,.2f}', 'Mes':'{:%m-%Y}', 'machine learning':'{:.2%}'}
#We make sure that the Month column has datetime format
df['Mes'] = pd.to_datetime(df['Mes'])
#We apply the style to the visualization
df.head().style.format(format_dict)
format_dict = {'Mes':'{:%m-%Y}'} #Simplified format dictionary with values that do make sense for our data
df.head().style.format(format_dict).highlight_max(color='darkgreen').highlight_min(color='#ff0000')
![](https://filescdn.proginn.com/b6f27736663fcbe3958be3f8658aefda/dfb831bfb0b09427fd2bcbf26deaced2.webp)
df.head(10).style.format(format_dict).background_gradient(subset=['data science', 'machine learning'], cmap='BuGn')
![](https://filescdn.proginn.com/5a8f5311410612a8dcb34660dbfef120/23f5f85d83b875101eb80710ab5ea93a.webp)
df.head().style.format(format_dict).bar(color='red', subset=['data science', 'deep learning'])
![](https://filescdn.proginn.com/a6518af4bca3b2ce8dfeaea7b1eccaf8/da1bc52d8991f00c2e16bdc1b204b41d.webp)
df.head(10).style.format(format_dict).background_gradient(subset = ['data science','machine learning'],cmap ='BuGn')。highlight_max(color ='yellow')
![](https://filescdn.proginn.com/92b2e5c2b2819cbff6f5da3ba20c4e1d/7722ef347fdf57f1b5657958e87b673d.webp)
from pandas_profiling import ProfileReport
prof = ProfileReport(df)
prof.to_file(output_file='report.html')
![](https://filescdn.proginn.com/31ee18b55196091a91b0468f6c6861e3/184c34dc6ab269365b44c50d63947df8.webp)
3. matplotlib
import matplotlib.pyplot as plt
plt.plot(df['Mes'], df['data science'], label='data science')
# The parameter label is to indicate the legend. This doesn't mean that it will be shown, we'll have to use another command that I'll explain later.
![](https://filescdn.proginn.com/e2ad893fb51517a3513f1e2d42700293/19ba8462122c233da2a4b0ef998db221.webp)
plt.plot(df ['Mes'],df ['data science'],label ='data science')
plt.plot(df ['Mes'],df ['machine learning'],label ='machine learning ')
plt.plot(df ['Mes'],df ['deep learning'],label ='deep learning')
![](https://filescdn.proginn.com/d72138a2ded98874033df00bbcf169f4/d8acca0904216367b59f83913e4c817e.webp)
plt.plot(df['Mes'], df['data science'], label='data science')
plt.plot(df['Mes'], df['machine learning'], label='machine learning')
plt.plot(df['Mes'], df['deep learning'], label='deep learning')
plt.xlabel('Date')
plt.ylabel('Popularity')
plt.title('Popularity of AI terms by date')
plt.grid(True)
plt.legend()
![](https://filescdn.proginn.com/52b8c5e3d1756f5f68c988976f6c8d20/ea17b881a63fd2140161734207ce21cb.webp)
fig, axes = plt.subplots(2,2)
axes[0, 0].hist(df['data science'])
axes[0, 1].scatter(df['Mes'], df['data science'])
axes[1, 0].plot(df['Mes'], df['machine learning'])
axes[1, 1].plot(df['Mes'], df['deep learning'])
![](https://filescdn.proginn.com/12be5921f4647ce28ec7679eaeb42c45/717e4e5a87aa52fd3b01d44df59cbbe1.webp)
plt.plot(df ['Mes'],df ['data science'],'r-')
plt.plot(df ['Mes'],df ['data science'] * 2,'bs')
plt .plot(df ['Mes'],df ['data science'] * 3,'g ^')
![](https://filescdn.proginn.com/fafc44633903c0b19f68fc940ea362ef/b0356c8db0e88453e812fbede06587d5.webp)
plt.scatter(df['data science'], df['machine learning'])
![](https://filescdn.proginn.com/2af63243174e3c5085114ca9a9d714b2/36753a9e40256fa89b3463886c2cf7f6.webp)
plt.bar(df ['Mes'],df ['machine learning'],width = 20)
![](https://filescdn.proginn.com/a4bc5a927e2eb25cf3635c6b1b9b8948/dc8aca55cb65a3a8f5176b3283633b53.webp)
plt.hist(df ['deep learning'],bins = 15)
![](https://filescdn.proginn.com/5aed817eec86330373e1c261920a06e9/8ef8b24d6ed6d5835a74f9b9829e751a.webp)
plt.plot(df['Mes'], df['data science'], label='data science')
plt.plot(df['Mes'], df['machine learning'], label='machine learning')
plt.plot(df['Mes'], df['deep learning'], label='deep learning')
plt.xlabel('Date')
plt.ylabel('Popularity')
plt.title('Popularity of AI terms by date')
plt.grid(True)
plt.text(x='2010-01-01', y=80, s=r'$\lambda=1, r^2=0.8$') #Coordinates use the same units as the graph
plt.annotate('Notice something?', xy=('2014-01-01', 30), xytext=('2006-01-01', 50), arrowprops={'facecolor':'red', 'shrink':0.05}
![](https://filescdn.proginn.com/54dd31f928bdc5c682842d6c2aa96c94/245c4dee9ea97de11f23f6c4d22daec0.webp)
4. seaborn
import seaborn as sns
sns.set()
sns.scatterplot(df['Mes'], df['data science'])
![](https://filescdn.proginn.com/15f08f8a3869693934887c7954c1c91e/e29d2401a10078d35eff8b620028097d.webp)
sns.relplot(x='Mes', y='deep learning', hue='data science', size='machine learning', col='categorical', data=df)
![](https://filescdn.proginn.com/fceabd3cb0ee793bf36f3eda6f313baf/16adb489f3217230f621e8806f675b2e.webp)
sns.heatmap(df.corr(),annot = True,fmt ='。2f')
![](https://filescdn.proginn.com/fca4ce2919a5343ac798aa8bc83fd569/a42d74934804109902ebb83d5f78d447.webp)
sns.pairplot(df)
![](https://filescdn.proginn.com/5dac1653cc7ca62f1b7e7d42188808f8/6ed3798dca05522451a7095d6c3e89ad.webp)
sns.pairplot(df,hue ='categorical')
![](https://filescdn.proginn.com/6f68058ed3da4ebc832d3360c803f2c1/a8f52d289504674a20fba66a753759d2.webp)
sns.jointplot(x='data science', y='machine learning', data=df)
![](https://filescdn.proginn.com/12796b7b01d82611275c04b0c98082a3/eddaf99c2e9362ffaf1e5c5c09ffa1dc.webp)
sns.catplot(x='categorical', y='data science', kind='violin', data=df)
![](https://filescdn.proginn.com/1f7fb8dc7c86f0471ac019555cb6f0b9/98ee839f55450782377dc3dc9d2b7d75.webp)
fig, axes = plt.subplots(1, 2, sharey=True, figsize=(8, 4))
sns.scatterplot(x="Mes", y="deep learning", hue="categorical", data=df, ax=axes[0])
axes[0].set_title('Deep Learning')
sns.scatterplot(x="Mes", y="machine learning", hue="categorical", data=df, ax=axes[1])
axes[1].set_title('Machine Learning')
![](https://filescdn.proginn.com/4b0a9d13cf1b0eaf52ba7932e6b02e70/30dec0ebb8d2da772c2b4023353196e4.webp)
5. Bokeh
from bokeh.plotting import figure, output_file, save
output_file('data_science_popularity.html')
p = figure(title='data science', x_axis_label='Mes', y_axis_label='data science')
p.line(df['Mes'], df['data science'], legend='popularity', line_width=2)
save(p)
![](https://filescdn.proginn.com/339d4003d9c3c423a50c5383e781e9c4/2ffd9c0b12e2cf3e20b0f7bc61750929.webp)
output_file('multiple_graphs.html')
s1 = figure(width=250, plot_height=250, title='data science')
s1.circle(df['Mes'], df['data science'], size=10, color='navy', alpha=0.5)
s2 = figure(width=250, height=250, x_range=s1.x_range, y_range=s1.y_range, title='machine learning') #share both axis range
s2.triangle(df['Mes'], df['machine learning'], size=10, color='red', alpha=0.5)
s3 = figure(width=250, height=250, x_range=s1.x_range, title='deep learning') #share only one axis range
s3.square(df['Mes'], df['deep learning'], size=5, color='green', alpha=0.5)
p = gridplot([[s1, s2, s3]])
save(p)
![](https://filescdn.proginn.com/9f7b02a2b98ff8ee8669128159a2e1cb/3eb402f889655cec0f6093b8589317c9.webp)
6. altair
7. folium
import folium
m1 = folium.Map(location=[41.38, 2.17], tiles='openstreetmap', zoom_start=18)
m1.save('map1.html')
![](https://filescdn.proginn.com/bb0a2011dceff3791662268de717352f/5fef77379368d888d1a6b2a356c27918.webp)
m2 = folium.Map(location=[41.38, 2.17], tiles='openstreetmap', zoom_start=16)
folium.Marker([41.38, 2.176], popup='<i>You can use whatever HTML code you want</i>', tooltip='click here').add_to(m2)
folium.Marker([41.38, 2.174], popup='<b>You can use whatever HTML code you want</b>', tooltip='dont click here').add_to(m2)
m2.save('map2.html')
![](https://filescdn.proginn.com/3fe2fe72e902ddcb2527d803d686a020/3158805843476a6ea90655fe3d7ac0af.webp)
from geopandas.tools import geocode
df2 = pd.read_csv('mapa.csv')
df2.dropna(axis=0, inplace=True)
df2['geometry'] = geocode(df2['País'], provider='nominatim')['geometry'] #It may take a while because it downloads a lot of data.
df2['Latitude'] = df2['geometry'].apply(lambda l: l.y)
df2['Longitude'] = df2['geometry'].apply(lambda l: l.x)
![](https://filescdn.proginn.com/b710c3cc701ca2d7020f0142af9601a2/5c56c676618d4bfce7c3f72a3eb23c43.webp)
m3 = folium.Map(location=[39.326234,-4.838065], tiles='openstreetmap', zoom_start=3)
def color_producer(val):
if val <= 50:
return 'red'
else:
return 'green'
for i in range(0,len(df2)):
folium.Circle(location=[df2.iloc[i]['Latitud'], df2.iloc[i]['Longitud']], radius=5000*df2.iloc[i]['data science'], color=color_producer(df2.iloc[i]['data science'])).add_to(m3)
m3.save('map3.html')
![](https://filescdn.proginn.com/0a8c2997eaf02bc1079ebd11b58a9edb/775229f68b508976ff98cf2dfc195226.webp)
扫码即可加我微信
即可获取最新学习资源
学习更多: 整理了我开始分享学习笔记到现在超过250篇优质文章,涵盖数据分析、爬虫、机器学习等方面,别再说不知道该从哪开始,实战哪里找了
“点赞”传统美德不能丢
评论
池州贵池区棠溪乡西山村小学
池州贵池区棠溪乡西山村小学,地址:安徽省池州市贵池区棠溪乡西山村,电话:4636001。汗水催桃李,沃土育新绿。学校追寻的是超前的工作态度,以勤、以诚、以梦想的力量,一步步走向辉煌。
池州贵池区棠溪乡西山村小学
0
池州棠溪乡中心小学
池州棠溪乡中心小学,地址:安徽省池州市贵池区棠溪乡双桥村,电话:4636296。百年沧桑、百年育人,其间人文荟萃、人才辈出,培育出“全国十佳好少年”,“省优秀少先队员”,“市十佳青少年”等一批品学兼优
池州棠溪乡中心小学
0