Python代碼|Python做數(shù)據(jù)可視化的代碼
前言:
從代碼中學習Python知識和Python與數(shù)據(jù)相關(guān)的知識,是一個有效的方法。例如:想了解Python做數(shù)據(jù)可視化的工作。我們可以從互聯(lián)網(wǎng)找一些Python做數(shù)據(jù)可視化的代碼進行閱讀,調(diào)試和遷移。這樣做的好處,突出實用性。同時,我們在結(jié)合聯(lián)想的學習方法,對所用到的可視化函數(shù),做個更深入地了解和使用。我借用《數(shù)據(jù)科學和人工智能》這個公眾號,分享一些我在實際的數(shù)據(jù)問題時,從網(wǎng)上找到的Python代碼,希望這些代碼對大家有作用和啟發(fā)。

Python做數(shù)據(jù)可視化代碼
#!/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代碼我經(jīng)過notebook調(diào)試測試通過的。
用到Python的庫有pandas, pandas_profiling, matplotlib, seaborn和bokeh。
配套的數(shù)據(jù)集和notebook下載鏈接。
http://47.112.229.252:80/f/97e449826d0a44468a8e/
我創(chuàng)建了Python語言群,需要加入的朋友,請掃碼添加我的微信,備注Python語言。
評論
圖片
表情
