8000字 | Python数据可视化,完整版实操指南 !
python之禅
共 11294字,需浏览 23分钟
· 2021-06-02
![](https://filescdn.proginn.com/cce2726f67293d1b7a4fdee3fe966115/3668bb35cf632b2cda78756a9a0628df.webp)
1. 前言
![](https://filescdn.proginn.com/81d72adc7fe25492e4f3de0192efd85f/bc1d4343c9be77a15e6190a56247e73b.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/d6c53f64a781c7523bce01889cedfdf5/cff476d460e407fe7089f5c24f7a255f.webp)
df.describe()
![](https://filescdn.proginn.com/8cae2ebe9f82a2c61fcc606a7aa62ae6/2060199d69016d1a53ffae536c38ba3a.webp)
df.info()
![](https://filescdn.proginn.com/7885c3a5da8d6dcc8794390db7497d24/df7364045fc4971b07e44ea0ddd8f16c.webp)
![](https://filescdn.proginn.com/9b3b261ee6c204c762f5a0265ed6b22f/3eb0f35d370f3b4acb48ad608458a573.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/4181fd911ae4ed3c9956190ec632c2f4/b3329b93f8ef1aaea548d053821968bc.webp)
df.head(10).style.format(format_dict).background_gradient(subset=['data science', 'machine learning'], cmap='BuGn')
![](https://filescdn.proginn.com/36dc89edd3ffa3a50cb7e1e8dd8f788f/ac4c804fa42825980f5375b62a266d1a.webp)
df.head().style.format(format_dict).bar(color='red', subset=['data science', 'deep learning'])
![](https://filescdn.proginn.com/3f20cd37ff6346d52824fd8559e3e5a1/b8a4e913ec52f38b495d94f42ea32a2e.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/87aa957e9ed9845dfa1c763ec61ba5b5/ae491d38437ba74c8ae00fae53c0b3f9.webp)
from pandas_profiling import ProfileReport
prof = ProfileReport(df)
prof.to_file(output_file='report.html')
![](https://filescdn.proginn.com/7d9939049b2da64b9f17e6eac10839f8/e32e54cd941d34209706bf38d511e7a8.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/d10464e0134141ef772bd4e42199e926/5ff789442196ca21698b445bce85efab.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/961797db639c591bbde6c6ad9456b656/43d893d12fbf2598527d9091a2acc3ec.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/e6e152ea5d60e5c19dfe1586acd91eeb/6f0d25bfa66cac232cc03b41d6bc7cd6.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/f82539572fb3ec1aa5c9ca0d305409cc/a4673bd9b72bd7006a43aa0de38a5a23.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/2c8a7e0d0feaedbe939544d769d51ad7/2402600d538874b1962a58061dd8f348.webp)
plt.scatter(df['data science'], df['machine learning'])
![](https://filescdn.proginn.com/74af2a64925dde58e29642a53bbe2eea/aac90b1f3961c3f0c883aaacaff493cf.webp)
plt.bar(df ['Mes'],df ['machine learning'],width = 20)
![](https://filescdn.proginn.com/3e60411c4a7593abae0dc77b551e0d28/cfd11b9ddc8730154e717add3b7d0885.webp)
plt.hist(df ['deep learning'],bins = 15)
![](https://filescdn.proginn.com/dc3847020c25246af9d5c57a68cba247/1d2a4b90cf6afebb571ea327f09ff5d5.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/a6487b8909a8d9b24c2e5e4f719cb1ea/c29f415d12e8f511d2b5cf0915657d2b.webp)
4. seaborn
import seaborn as sns
sns.set()
sns.scatterplot(df['Mes'], df['data science'])
![](https://filescdn.proginn.com/be9d9d2f2364d0adcad733c4d5975888/f455950b928cc33d5e8c5f02c5401fd3.webp)
sns.relplot(x='Mes', y='deep learning', hue='data science', size='machine learning', col='categorical', data=df)
![](https://filescdn.proginn.com/62ebd1a8290994e48d1229c0590d8343/1c8e636c11a7a01e34aa109f9e3107a8.webp)
sns.heatmap(df.corr(),annot = True,fmt ='。2f')
![](https://filescdn.proginn.com/55e05dd02e27792fbaddc5126dd2052b/b5697ed69f1c9ccbad683286ac6d89ff.webp)
sns.pairplot(df)
![](https://filescdn.proginn.com/84d56023bb6c32c72c4ba14184f92acd/6280e178bae82248b5f3ae21bf8297c4.webp)
sns.pairplot(df,hue ='categorical')
![](https://filescdn.proginn.com/44955858541ec7643060bb8f825cf5c6/1c21ed9648d63d1b6dc524a588fe06d8.webp)
sns.jointplot(x='data science', y='machine learning', data=df)
![](https://filescdn.proginn.com/68c55be499103cb9021aa29b68e060ec/0ed814b44661f3bcf10da5ff5ed40d66.webp)
sns.catplot(x='categorical', y='data science', kind='violin', data=df)
![](https://filescdn.proginn.com/f6c4c6dec04fc5e0130dca2fb28e7c4e/e86f99810b187b9291bd18b2b8bf5f2f.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/0ce22ee9d6f3b8a4c3181bafc63f4756/1daa8ba1f52a1e1da7107ac10ef67af5.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/8907f82af6f33e997ba933e31c48ce27/dd5d55492a7394c728362498f7034c22.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/9140a9387b2449172f4528773ca27116/220c7c0dbda895ce589bbb10bbb18f20.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/a66cda598d6961861421ac14cd5c3730/950295c9680a3d565a2ec86f15e8fd0c.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/d1422c403b58ff1e40f54202ba92d68b/6be76330504b48fd2f2acf589fd07074.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/509c014584ec2d1a135140f5c755481d/b32c68d55d7ab8e0563bf3b9e7ac4d7f.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/11d4ab6b94251cbf02cd1c8267ec3c31/aeafc3d21e1d54c3ae76b991887ecc46.webp)
- EOF -
推荐阅读:
评论
林州泽下乡泽下小学
林州泽下乡泽下小学,地址:河南省林州市五龙镇泽下村,电话:6998020。该校是一所省级家教名校、读书活动先进单位、市传统文化活动先进单位、市文明单位、市宣传思想工作先进单位、市平安单位、市合格学校、
林州泽下乡泽下小学
0
林州泽下乡马兰小学
林州泽下乡马兰小学,地址:河南省林州市五龙镇岭后村,电话:6997177。百年沧桑、百年育人,其间人文荟萃、人才辈出,培育出“全国十佳好少年”,“省优秀少先队员”,“市十佳青少年”等一批品学兼优的好学
林州泽下乡马兰小学
0
林州泽下乡岭南小学
林州泽下乡岭南小学,地址:河南省林州市五龙镇岭南村,电话:6993552。建设了校园闭路电视系统,实现了班班通电视,购置了全套的视频编辑播放设备,逐步使我校现代教育技术纳入系统化、规范化、实效化的轨道
林州泽下乡岭南小学
0