import numpy as np
import matplotlib.pyplot as plt
# 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(0, 100, 1000)
y = np.sin(x)
plt.plot(x, y, c='red', lw=2, ls='-')
plt.show()
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, lw=2, ls='-', label='X和Y的关系')
plt.legend(loc='upper center') # 图例
plt.show()
x = np.arange(0, 1.1, 0.01)
y = x ** 2
plt.figure(figsize=(6.4, 4.84), dpi=100, facecolor='white')
plt.title('这是一幅图')
plt.xlabel('x1')
plt.ylabel('y')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.plot(x, y, label='Y = x ^ 2')
plt.legend(loc='best')
plt.savefig('地址') # 图形保存
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('2018-2010_export.csv')
data_1 = data[:277].groupby('country').mean()['value']
print(data_1)
print(data_1.index)
x_data = data_1.values
y_data = data_1.index
explode = [0, 0.1, 0, 0, 0, 0.2]
colors = ['red', 'blue', 'yellow', 'green', 'darkred', 'purple']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.pie(x=x_data, explode=explode, labels=y_data, colors=colors, autopct='.1f%%', pctdistance=0.5, labeldistance=1.1,
startangle=120, radius=1.2, counterclock=False, wedgeprops={'linewidth': 1.5, 'edgecolor': 'green'},
textprops={'fontsize': 10, 'color': 'black'})
plt.title('饼图', pad=30)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('2018-2010_export.csv')
data_1 = data[:277].groupby('country').mean()['value']
x_data = data_1.values
y_labels = data_1.index
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.bar(x=range(0, len(x_data)), height=x_data, align='center', color='y', tick_label=y_labels)
plt.xlabel('地区')
plt.ylabel('销量')
plt.title('条形图')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('2018-2010_export.csv')
data_1 = data[:277].groupby('country').mean()['value']
x_data = data_1.values
y_labels = data_1.index
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.hist(x=data['value'], bins=6, color='r', edgecolor='black', density=True)
plt.title('直方图')
plt.show()
数据不太好,代码没问题,所以不展示图片了
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('2018-2010_export.csv')
data_1 = data[:277]
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.scatter(x=data.index, y=data['value'], marker='o', s=10)
plt.xlabel('x', fontsize=12)
plt.ylabel('y', fontsize=12)
plt.title('关系', fontsize=12)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('2018-2010_export.csv')
data_1 = data[:277].groupby('country').mean()['value']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.plot(data_1.index, data_1.values, label='不同地区销售')
# 图例
plt.legend(loc='upper left', fontsize=12, frameon=False)
# 图像大小
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
# 网格线
plt.grid(ls='--', c='darkblue')
# 水平参考线
plt.axhline(y=1700, c='red', ls='--', lw=2)
plt.axvline(x=4, c='red', ls='--', lw=2)
# 水平参考区域
plt.axvspan(xmin=4, xmax=6, facecolor='r', alpha=0.3)
plt.axhspan(ymin=250, ymax=1250, facecolor='blue', alpha=0.3)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
GDP = pd.read_csv('gdp-csv-.csv', encoding='gbk')[:10]
plt.figure(figsize=(6.4, 4.8))
plt.bar(x=GDP.country, height=GDP.GDP, width=0.5)
plt.xticks(GDP.index, GDP.country, rotation=45)
plt.xlabel('国家', fontsize=12)
plt.ylabel('GDP产值(万亿)', fontsize=12, labelpad=20)
plt.title('GDP情况', fontsize=20)
plt.show()
# 堆叠图
Industry_GDP = pd.read_excel('Industry_GDP.xlsx')
# temp = pd.pivot_table(data=Industry_GDP, index='Quarter', columns='Industry_Type', values='GDP', aggfunc=np.sum)
temp = pd.crosstab(Industry_GDP['Quarter'], Industry_GDP['Industry_Type'], values=Industry_GDP['GDP'], aggfunc=np.sum, normalize='index')
plt.bar(x=temp.index.values, height=temp['第一产业'], color='steelblue', label='第一产业', tick_label=['第一季度', '第二季度', '第三季度', '第四季度'])
plt.bar(x=temp.index.values, height=temp['第二产业'], color='green', label='第二产业', bottom=temp['第一产业'], tick_label=['第一季度', '第二季度', '第三季度', '第四季度'])
plt.bar(x=temp.index.values, height=temp['第三产业'], color='red', label='第三产业', bottom=temp['第一产业'] + temp['第二产业'], tick_label=['第一季度', '第二季度', '第三季度', '第四季度'])
plt.ylabel('产值(亿)')
plt.title('2017年各产业季度生产值')
# plt.legend(loc='best')
plt.legend(bbox_to_anchor=(1.08, 0.8)) # 图例放在外面
plt.show()
数据不太好,代码没问题,不展示图了
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
Titanic = pd.read_csv('train_and_test2.csv')
Titanic.dropna(subset=['Age'], inplace=True)
def normfun(x, mu, sigma):
pdf = np.exp(-((x - mu)**2)/(2*sigma**2)) / (sigma * np.sqrt(2*np.pi))
return pdf
mean_x = Titanic['Age'].mean()
std_x = Titanic['Age'].std()
x = np.arange(Titanic['Age'].min(), Titanic['Age'].max() + 10, 1)
y = normfun(x, mean_x, std_x)
plt.hist(x=Titanic['Age'], bins=20, color='orange', edgecolor='red', density=True, label='直方图')
plt.plot(x, y, color='g', linewidth=3, label='正态分布图')
Titanic['Age'].plot(kind='kde', color='red', xlim=[0, 90], label='核密度图')
plt.xlabel('年龄', fontsize=15)
plt.ylabel('频率', fontsize=15)
plt.title('年龄分布图', pad=20)
plt.legend(loc='best')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
sec_buildings = pd.read_csv('buildings.csv')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.boxplot(x=sec_buildings['2018energyusintensity'][:200], patch_artist=True, showmeans=True, showfliers=True,
boxprops={'color': 'black', 'facecolor': 'steelblue'},
flierprops={'marker': 'o', 'markerfacecolor': 'red', 'markersize': 5},
meanprops={'marker': 'D', 'markerfacecolor': 'indianred', 'markersize': 4},
medianprops={'linestyle': '--', 'color': 'orange'})
plt.title('箱型图')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
Iris = pd.read_csv('Iris.csv')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(10, 8))
plt.scatter(x=Iris.PetalWidthCm, y=Iris.PetalLengthCm, s=10, color='steelblue')
plt.xlabel('花瓣宽度')
plt.ylabel('花瓣长度')
plt.title('长度和宽度关系')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import os
os.chdir(r'C:\Users\Administrator.DESKTOP-0L8IC5U\Desktop\数据分析\data')
data = pd.read_csv('database.csv')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(8, 7))
plt.plot(data.iloc[:, 1], data.iloc[:, 10], 'bs--', data.iloc[:, 1], data.iloc[:, 11], 'ro--',)
plt.xlabel('年份', labelpad=25)
plt.ylabel('值')
plt.legend(['Engine Cylinders', 'Engine Displacement'])
plt.show()
数据不太好,代码没问题,不展示图了