- 【Python数据分析与可视化】Pandas数据分析与matplotlib绘图
- 导包
- matplotlib模仿绘制图表
- 如下样式的绘图,请写出相应的代码;
- 如下样式的绘图,请写出相应的代码;
- 数据分析及可视化
- 查看星巴克旗下有哪些品牌,统计每个品牌有多少家店
- 查看全世界一共有多少个国家和地区开设了星巴克门店,显示门店数量排名前10
- 用柱状图绘制排名前10国家的分布情况
- 显示拥有星巴克门店数量排名前10的城市
- 绘制星巴克门店数量前10的城市分布线型图
- 显示拥有星巴克门在中国的分布情况,店数量排名前10的城市
- 绘制星巴克中国门店数量前10的城市分布柱状图
- 用饼状图显示星巴克门店的经营方式有哪几种?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams["font.family"]=['SimHei']
plt.rcParams['axes.unicode_minus']=False
matplotlib模仿绘制图表
如下样式的绘图,请写出相应的代码;
fig=plt.figure(figsize=(16,10))
ax1=plt.subplot(2,2,1)
ax2=plt.subplot(2,2,2)
ax3=plt.subplot(2,2,3)
plt.tight_layout(h_pad=2)
x1=np.arange(0,5,0.001)
y1=np.sin(2*np.pi*x1)
ax1.set_xlabel("X轴")
ax1.set_ylabel("Y轴")
ax1.set_xticks([0,1,2,3,4,5])
ax1.set_ylim(-2,2)
ax1.set_yticks([i for i in np.arange(-2.0,2.1,0.5)])
ax1.annotate('local max', xy=(2.2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='red', shrink=0.05),
bbox={'facecolor':'yellow', 'alpha':0.5, 'pad':2} )
ax1.text(1, 1.2, 'Max', style='italic',fontsize='large')
ax1.text(2, -1.5, 'y=sin(2*np.pi*t)', style='italic',
bbox={'facecolor':'yellow', 'alpha':0.5, 'pad':2},rotation=15)
ax1.plot(x1,y1)
x2=np.arange(0,np.pi*2,0.01)
y2=np.sin(x2)
ax2.set_xticks([0,np.pi/2,np.pi,3*np.pi/2,2*np.pi] )
ax2.set_xticklabels(('0', 'π/2', 'π', '3π/2', '2π'))
ax2.set_ylim(-1.5,1.5)
ax2.set_title('y=sin(x)',y=-0.14,bbox=dict(facecolor='w',alpha=0.65))
ax2.annotate('sin(x)最小值', xy=(4.71, -1), xytext=(4,-0.2),
arrowprops=dict(facecolor='red',arrowstyle="->",edgecolor='r'),
bbox={'facecolor':'yellow', 'alpha':0.5, 'pad':2} )
ax2.plot(x2,y2,"-.",color='#a10cc7')
x3=np.arange(0,np.pi*2,0.01)
y3=np.cos(x3)
ax3.set_ylim(-1.5,1.5)
ax3.set_xticks([0,np.pi/2,np.pi,3*np.pi/2,2*np.pi] )
ax3.set_xticklabels(('0', 'π/2', 'π', '3π/2', '2π'))
ax3.set_title('y=cos(x)',y=-0.14,bbox=dict(facecolor='w',alpha=0.65))
ax3.annotate('cos(x)最小值', xy=(3.14, -1), xytext=(3.2,0.2),
arrowprops=dict(facecolor='red',arrowstyle="->",edgecolor='r'),
bbox={'facecolor':'yellow', 'alpha':0.5, 'pad':2} )
ax3.plot(x3,y3,"--",color='#a10cc7')
plt.show()
如下样式的绘图,请写出相应的代码;-
- 绘图函数:y=sin(x), y=cos(x), x = np.linspace(-np.pi, np.pi, 256, endpoint=True);
-
- 绘制填充区域: 紫色区域:(-2.5
0.5
- 绘制填充区域: 紫色区域:(-2.5
x=np.linspace(-1*np.pi,np.pi,256,endpoint=True)
y1=np.sin(x)
y2=np.cos(x)
# 绘图
plt.figure(figsize=(10,7))
plt.plot(x,y1,'b')
plt.plot(x,y2,'r')
#填充颜色
plt.fill_between(x,y1,where=(y1>0.5),facecolor='green')
plt.fill_between(x,y2,where=(x>-2.5)&(x<-0.5),facecolor='purple')
#图例
plt.legend(['sin(x)','cos[x]'],loc='upper left')
#网格
plt.grid(True)
#设置坐标轴
plt.xticks([-1*np.pi,-1*np.pi/2,0,np.pi/2,np.pi],('-Π', '-π/2', '0', 'π/2', 'π'))
plt.yticks([-1,1])
axes=plt.gca()
axes.spines['top'].set_color('none')
axes.spines['right'].set_color('none')
axes.xaxis.set_ticks_position('bottom')
axes.spines['bottom'].set_position(('data', 0))
axes.yaxis.set_ticks_position('left')
axes.spines['left'].set_position(('data', 0))
#标题
plt.title('绘图实例之SIN()&COS()',color='g')
#标注
plt.annotate('sin(2Π/3)=√3/2', xy=(np.pi*2/3, 0.85), xytext=(2.5,1),
arrowprops=dict(facecolor='black',arrowstyle="->",edgecolor='black'))
plt.annotate('cos(-Π)=-1', xy=(-1*np.pi, -1), xytext=(-2.5,-1.2),
arrowprops=dict(facecolor='black',arrowstyle="->",edgecolor='black',shrinkB=0,
connectionstyle="arc3,rad=0.3"))
plt.text(2, -1.2, 'python_matplotlib',color='purple')
plt.show()
- 实训针对一组关于全球星巴克门店的统计数据(directory.csv),
分析了在不同国家和地区以及中国不同城市的星巴克门店的数量;
df=pd.read_csv('directory.csv')
- 读取数据:显示前5行数据
df.head()
Brand | Store Number | Store Name | Ownership Type | Street Address | City | State/Province | Country | Postcode | Phone Number | Timezone | Longitude | Latitude | Unnamed: 13 | Unnamed: 14 | Unnamed: 15 | Unnamed: 16 | Unnamed: 17 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Starbucks | 47370-257954 | Meritxell, 96 | Licensed | Av. Meritxell, 96 | Andorra la Vella | AJ | AD | AD500 | 376818720 | GMT+1:00 Europe/Andorra | 1.53 | 42.51 | NaN | NaN | NaN | NaN | NaN |
1 | Starbucks | 22331-212325 | Ajman Drive Thru | Licensed | 1 Street 69, Al Jarf | Ajman | AJ | AD | NaN | NaN | GMT+04:00 Asia/Dubai | 55.47 | 25.42 | NaN | NaN | NaN | NaN | NaN |
2 | Evolution Fresh | 47089-256771 | Dana Mall | Licensed | Sheikh Khalifa Bin Zayed St. | Ajman | AJ | AD | NaN | NaN | GMT+04:00 Asia/Dubai | 55.47 | 25.39 | NaN | NaN | NaN | NaN | NaN |
3 | Starbucks | 22126-218024 | Twofour 54 | Licensed | Al Salam Street | Abu Dhabi | AZ | AD | NaN | NaN | GMT+04:00 Asia/Dubai | 54.38 | 24.48 | NaN | NaN | NaN | NaN | NaN |
4 | Starbucks | 17127-178586 | Al Ain Tower | Licensed | Khaldiya Area, Abu Dhabi Island | Abu Dhabi | AZ | AD | NaN | NaN | GMT+04:00 Asia/Dubai | 54.54 | 24.51 | NaN | NaN | NaN | NaN | NaN |
print("星巴克旗下品牌有:")
df['Brand'].value_counts()
星巴克旗下品牌有:
Starbucks 6710
Teavana 97
Evolution Fresh 22
Coffee House Holdings 15
Name: Brand, dtype: int64
查看全世界一共有多少个国家和地区开设了星巴克门店,显示门店数量排名前10
和后10的国家
print("全世界一共有多少个国家开设了星巴克门店",len(df['Country'].unique()))
print("排名前十的国家和数量为:")
country_count = df['Country'].value_counts()
print(country_count[:10])
print("排名后十的国家和数量为:")
print(country_count[-10:])
全世界一共有多少个国家开设了星巴克门店 20
排名前十的国家和数量为:
CN 1546
US 1267
AT 1174
BR 778
AU 745
AZ 525
CA 228
BE 154
AR 148
AE 144
Name: Country, dtype: int64
排名后十的国家和数量为:
GR 28
BN 27
AW 24
HU 16
BS 10
GT 7
AD 7
BH 7
BG 5
BO 4
Name: Country, dtype: int64
用柱状图绘制排名前10国家的分布情况
plt.figure(figsize=(10,5))
labels = list(country_count[:10].index) # 刻度标签
plt.xlabel('国家')
plt.ylabel('门店数量')
plt.title('星巴克门店数量排名前10的国家')
plt.bar([i for i in labels], country_count[:10])
plt.show()
显示拥有星巴克门店数量排名前10的城市print("排名前十的城市和数量为:")
city_count = df['City'].value_counts()
print(city_count[:10])
排名前十的城市和数量为:
Beijing 342
Shanghai 279
Changchun 195
Suzhou 157
Buenos Aires 148
San Diego 135
Los Angeles 127
Seattle 126
Dalian 119
Guangzhou 119
Name: City, dtype: int64
绘制星巴克门店数量前10的城市分布线型图
plt.figure(figsize=(10,5))
labels = list(city_count[:10].index) # 刻度标签
plt.xlabel('城市')
plt.ylabel('门店数量')
plt.title('星巴克门店数量排名前10的城市')
plt.plot([i for i in labels], city_count[:10])
plt.show()
显示拥有星巴克门在中国的分布情况,店数量排名前10的城市cn_df = df[df['Country'] == 'CN']
cn_city_count=cn_df['City'].value_counts()
print("排名前十的中国城市和数量为:\nCity")
print(cn_city_count[:10])
排名前十的中国城市和数量为:
City
Beijing 342
Shanghai 279
Changchun 195
Suzhou 157
Guangzhou 119
Dalian 119
Zhengzhou 98
Qingdao 89
Shijiazhuang 55
Shenzhen 48
Name: City, dtype: int64
绘制星巴克中国门店数量前10的城市分布柱状图
plt.figure(figsize=(10,5))
labels = list(cn_city_count[:10].index) # 刻度标签
plt.xlabel('城市')
plt.ylabel('门店数量')
plt.title('星巴克门店数量排名前10的国家')
plt.bar([i for i in labels], cn_city_count[:10])
plt.show()
用饼状图显示星巴克门店的经营方式有哪几种?manage_count=df['Ownership Type'].value_counts()
plt.figure(figsize=(10,7))
plt.pie(manage_count,labels=manage_count.index)
plt.title("星巴克门店的经营方式")
plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)