import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
tips = sns.load_dataset("tips")
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

箱线图

plt.boxplot(x = tips[tips['sex']=='Female']['total_bill'])
plt.show()
sns.boxplot(x = 'total_bill', y = 'sex', data = tips)
plt.show()

Python画图

Python画图

散点图

##### plt  画图
plt.scatter(tips['total_bill'], tips['tip'], color='green', marker='o')
# plt.xlim(np.floor(min(tips['total_bill'])), np.ceil(max(tips['total_bill'])))
# plt.xlim(np.floor(min(tips['tip'])), np.ceil(max(tips['tip'])))
# plt.plot(np.mean(tips['tip']),color = 'red')
plt.title('total_bill VS tip')
plt.xlabel('total_bill')
plt.ylabel('tip')
plt.show()
#### sns 画图
sns.scatterplot(x = 'total_bill', y = 'tip', data = tips, markers= '*')
plt.title('sns___total_bill VS tip')
plt.show()

Python画图

Python画图

折线图

plt.plot(tips['total_bill'], color = 'red')
plt.title('plt_total_bill line')
plt.show()
plt.plot(tips['tip'], color = 'green')
plt.show()

Python画图

Python画图

双坐标轴

ax1=plt.subplot(111)
# tips['tip'].plot(ax=ax1,color='b')
ax1.plot('tip', data = tips, color = 'b')
ax1.set_ylabel('tip')
# 重点来了,twinx 或者 twiny 函数
ax2 = ax1.twinx()
# tips['total_bill'].plot(ax=ax2,color='r')
ax2.plot('total_bill', data = tips, color = 'r')
ax2.set_ylabel('total_bill')
ax1.set_label('double label for total_bill and tip')

Python画图

柱状图


plt.bar(height = tips['tip'], x = tips['day'], color = 'g')
plt.show()

## sns.barplot()
sns.barplot(y = 'tip', x = 'day', data = tips,estimator= np.sum, palette='Blues_d')
plt.title('the total tip of each day')
# for a,b in zip(tips['day'], tips['tip']):
#     plt.text(a, b+0.1,'%.0f'%b,ha = 'center',va = 'bottom',fontsize=7)
plt.show()

ax = sns.barplot(x = 'time', y = 'tip', data = tips,order = ['Dinner', 'Lunch'],estimator= np.sum)
plt.title('the total tip of each time order by ---Dinner, Luach')
plt.show()
###sns.countplot()
sns.countplot('day', data = tips, palette= 'Set3')
plt.title('the frequency of day')
plt.show()

Python画图

C:\Users\Administrator\software\anoconda\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

Python画图

Python画图

Python画图

创建带数字标签的直方图

numbers = list(range(1,11))
#np.array()将列表转换为存储单一数据类型的多维数组
x = np.array(numbers)
y = np.array([a**2 for a in numbers])
plt.bar(x,y,width=0.5,align='center',color='c')
plt.title('Square Numbers',fontsize=24)
plt.xlabel('Value',fontsize=14)
plt.ylabel('Square of Value',fontsize=14)
plt.tick_params(axis='both',labelsize=14)
plt.axis([0,11,0,110])
for a,b in zip(x,y):
    plt.text(a,b+0.1,'%.0f'%b,ha = 'center',va = 'bottom',fontsize=7)
plt.show()

Python画图

#模块pyplot包含很多生成图表的函数
input_values = [1,2,3,4,5,6]
squares = [1,4,9,16,25,36]
#plot()绘制折线图
plt.plot(input_values,squares,linewidth=5)
#np.array()将列表转换为存储单一数据类型的多维数组
x = np.array(input_values)
y = np.array(squares)
#annotate()给折线点设置坐标值
for a,b in zip(x,y):
    plt.annotate('(%s,%s)'%(a,b),xy=(a,b),xytext=(-20,10),
                 textcoords='offset points')
#设置标题
plt.title('Square Numbers',fontsize=24)
plt.xlabel('Value',fontsize=14)
plt.ylabel('Square of Value',fontsize=14)
#设置刻度的大小,both代表xy同时设置
plt.tick_params(axis='both',labelsize=14)
#show()打开matplotlib查看器,并显示绘制的图形
plt.show()

Python画图

day = tips.groupby('day', as_index = False)['tip'].sum()
day.sort_values(by = 'tip',ascending=False, inplace=True)
day

res = plt.bar(height = day['tip'], x = day['day'], color = 'g')
plt.title('the total tip of each day')
for r in res:
    b = r.get_height()
#     plt.text(r.get_x()+ r.get_width()/2, r.get_height(), '%.0f'r.get_height(), ha='center', fontsize=7 )
    plt.text(r.get_x()+ r.get_width()/2, r.get_height(),'%.0f'%b,ha = 'center',va = 'bottom',fontsize=20)
plt.ylim(0,300)
plt.show()

Python画图

相关文章:

  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-03
  • 2021-12-18
  • 2021-07-02
相关资源
相似解决方案