Python代码|Python做数据可视化的代码
数据科学与人工智能
共 5362字,需浏览 11分钟
· 2020-06-26
前言:
从代码中学习Python知识和Python与数据相关的知识,是一个有效的方法。例如:想了解Python做数据可视化的工作。我们可以从互联网找一些Python做数据可视化的代码进行阅读,调试和迁移。这样做的好处,突出实用性。同时,我们在结合联想的学习方法,对所用到的可视化函数,做个更深入地了解和使用。我借用《数据科学和人工智能》这个公众号,分享一些我在实际的数据问题时,从网上找到的Python代码,希望这些代码对大家有作用和启发。
Python做数据可视化代码
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
df = pd.read_csv('./datasets/temporal.csv')
df.head(10)
# In[2]:
df.describe()
# In[3]:
df.info()
# In[5]:
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# In[6]:
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)
# In[7]:
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')
# In[8]:
df.head(10).style.format(format_dict).background_gradient(subset=['data science', 'machine learning'], cmap='BuGn')
# In[9]:
df.head().style.format(format_dict).bar(color='red', subset=['data science', 'deep learning'])
# In[10]:
df.head(10).style.format(format_dict).background_gradient(subset=['data science', 'machine learning'], cmap='BuGn').highlight_max(color='yellow')
# In[11]:
from pandas_profiling import ProfileReport
prof = ProfileReport(df)
prof.to_file(output_file='report.html')
# In[17]:
import matplotlib.pyplot as plt
#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.
plt.plot(df['Mes'], df['data_science'], label='data science')
# In[14]:
df
# In[18]:
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')
# In[19]:
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()
# In[20]:
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'])
# In[21]:
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^')
# In[23]:
plt.scatter(df['data_science'], df['machine_learning'])
# In[24]:
plt.bar(df['Mes'], df['machine_learning'], width=20)
# In[25]:
plt.hist(df['deep_learning'], bins=15)
# In[26]:
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})
# In[28]:
import seaborn as sns
sns.set()
sns.scatterplot(df['Mes'], df['data_science'])
# In[29]:
sns.relplot(x='Mes', y='deep_learning', hue='data_science', size='machine_learning', col='categorical', data=df)
# In[30]:
sns.heatmap(df.corr(), annot=True, fmt='.2f')
# In[31]:
sns.pairplot(df)
# In[32]:
sns.pairplot(df, hue='categorical')
# In[34]:
sns.jointplot(x='data_science', y='machine_learning', data=df)
# In[35]:
sns.catplot(x='categorical', y='data_science', kind='violin', data=df)
# In[36]:
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')
# In[37]:
from bokeh.plotting import figure, output_file, save
output_file('data_science_popularity.html')
# In[38]:
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)
# In[40]:
from bokeh.layouts import gridplot
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://towardsdatascience.com/complete-guide-to-data-visualization-with-python-2dd74df12b5e
这份Python代码我经过notebook调试测试通过的。
用到Python的库有pandas, pandas_profiling, matplotlib, seaborn和bokeh。
配套的数据集和notebook下载链接。
http://47.112.229.252:80/f/97e449826d0a44468a8e/
我创建了Python语言群,需要加入的朋友,请扫码添加我的微信,备注Python语言。
评论
盘点Lombok的几个骚操作,你绝对没用过!
👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目 2.0 版本完结啦, 演示链接:http://116.62.199.48/ ,新项目正在酝酿中
小哈学Java
0
堪称最优秀的Docker可视化管理工具——Portainer你真的会用吗?
来源:blog.csdn.net/shark_chili3007/article/details/123366179👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目
小哈学Java
0
JS的这些新特性,你都用过么?
大厂技术 高级前端 Node进阶点击上方 程序员成长指北,关注公众号回复1,加入高级Node交流群作为一门不断演进的语言,JavaScript每年都会引入新特性。这些特性的加入,能够帮助我们编写更加简洁、高效、易于维护的代码。然而,并非所有新特性
程序员成长指北
1
【深度学习】人人都能看懂的LSTM
熟悉深度学习的朋友知道,LSTM是一种RNN模型,可以方便地处理时间序列数据,在NLP等领域有广泛应用。在看了台大李宏毅教授的深度学习视频后,特别是介绍的第一部分RNN以及LSTM,整个人醍醐灌顶。本文就是对视频的记录加上了一些个人的思考。0. 从RNN说起循环神经网络(Recurrent Neur
机器学习初学者
0
我发现 Lombok的几个骚操作,哈哈好用
大家好,我是小富~前言本文不讨论对错,只讲骚操作。有的方法看看就好,知道可以这么用,但是否应用到实际开发中,那就仁者见仁,智者见智了。一万个读者就会有一万个哈姆雷特,希望这篇文章能够给您带来一些思考。耐心看完,你一定会有所收获。@onX例如 onConstructor, oMet
程序员内点事
0
测试新人,如何快速上手一个陌生的系统!
大家好,我是狂师!作为刚入行不久的测试新人,面对一个陌生的系统时,可能会感到有些手足无措。面对一个全新的系统系统,如何快速上手并展开有效的测试工作是一个重要的挑战。本文将探讨测试新人如何通过一系列步骤和策略,快速熟悉并掌握新系统的测试要点,从而提高测试效率和质量。本文旨在为测试新手提供一份指导,帮助
测试开发技术
0
如何计算数据中心的冷却需求?
今日分享 【导读】数据中心的冷却要求受多种因素影响,包括设备的热量输出、占地面积、设施设计和电气系统功率额定值等等……众所周知,环境因素会严重影响数据中心设备。过多的热量积聚会损坏服务器,可能导致其自动关闭。经常在高于可接受的温度下运行服务器会缩短其使用
数据中心运维管理
0
多人同时导出 Excel 干崩服务器!新来的阿里大佬给出的解决方案太优雅了!
点击关注公众号,Java 干货及时推送↓推荐阅读:面试辅导,我们出大成果了!来源:juejin.cn/post/7259249904777838629前言 业务诉求:考虑到数据库数据日渐增多,导出会有全量数据的导出,多人同时导出可以会对服务性能造成影响,导出涉及到mysql查询的io操作,
Java技术栈
1