pandas简单绘制图表

pandas简单绘制图表,第1张

在阅读本篇博文前请详细阅读这篇前置博文

pandas的简单使用_wolfwalker的博客-CSDN博客

由于我们的pandas内部有画图的方法,所以我们可以很方便地生成具体的图像

一、绘制折线图 
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
data={'上海':[27677,30133,32679],
      '北京':[24899,28000,30320],
      '深圳':[19600,22286,24961],
      '广州':[19493,21500,23000]}
gdp=pd.DataFrame(data,index=[2016,2017,2018])

series2=pd.Series([17559,19530,20363],index=[2016,2017,2018])
series2.index.name='重庆\'GDP'
series2.name='重庆'
gdp['重庆']=series2

gdp.index.name='年份'
gdp.columns.name='城市'
gdp.plot()
plt.show()

 二、绘制柱状图
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
data={'上海':[27677,30133,32679],
      '北京':[24899,28000,30320],
      '深圳':[19600,22286,24961],
      '广州':[19493,21500,23000]}
gdp=pd.DataFrame(data,index=[2016,2017,2018])

series2=pd.Series([17559,19530,20363],index=[2016,2017,2018])
series2.index.name='重庆\'GDP'
series2.name='重庆'
gdp['重庆']=series2

gdp.index.name='年份'
gdp.columns.name='城市'
gdp.plot.bar()
plt.show()

三、绘制条形图 
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
data={'上海':[27677,30133,32679],
      '北京':[24899,28000,30320],
      '深圳':[19600,22286,24961],
      '广州':[19493,21500,23000]}
gdp=pd.DataFrame(data,index=[2016,2017,2018])

series2=pd.Series([17559,19530,20363],index=[2016,2017,2018])
series2.index.name='重庆\'GDP'
series2.name='重庆'
gdp['重庆']=series2

gdp.index.name='年份'
gdp.columns.name='城市'
gdp.plot.bar()
plt.show()

 

四、使用seaborn美化我们的图表 绘制纵向的柱状图

将我们orient='v'设置之后,我们的图像就是纵向的

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False


n=[response['城市'],response['2018年']]
m=pd.concat(n,axis=1)
fig=plt.figure(figsize=(60,5))
sns.barplot(x='城市',y='2018年',data=response,orient='v')
plt.show()

 绘制横向的柱状图

将我们orient='h'设置之后,我们的图像就是横向的


n=[response['城市'],response['2018年']]
m=pd.concat(n,axis=1)
fig=plt.figure(figsize=(5,8))
sns.barplot(x='2018年',y='城市',data=response,orient='h')
plt.savefig('1.png')
plt.show()

 五、绘制直方图

使用.plot.hist(bins=)的方法绘制直方图


n=[response['城市'],response['2018年']]
m=pd.concat(n,axis=1)
fig=plt.figure(figsize=(5,8))
response.plot.hist(bins=50)
plt.savefig('1.png')
plt.show()

 六、密度图

使用plot.density绘制密度图

n=[response['城市'],response['2018年']]
m=pd.concat(n,axis=1)
# fig=plt.figure(figsize=(5,8))
# sns.barplot(x='2018年',y='城市',data=response,orient='h')
response.plot.density()
plt.savefig('1.png')
plt.show()

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/916897.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存