python 画图

1、给图形添加数据标签

plt.plot(datat.index,datat)
plt.xlabel('index', fontsize=15)
plt.legend(['t_bottom','t_top'],loc = 'upper_right',fontsize = 10)
plt.show()

python 画图_第1张图片

2、将标签置于最右边

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

 

python 画图_第2张图片

3、显示中文字体出现方块

# coding: UTF-8
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号#有中文出现的情况,需要u'内容

4、画热力图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
d ={'1':[0.87,0.95],'2':[0.86,0.91],'3':[0.86,0.93],'4':[0.86,0.92],'5':[0.86,0.93]}
df = pd.DataFrame(d,index = [2,1])
df
### annot是表示显示方块代表的数值出来 cmap颜色
sns.heatmap(df,annot = True,cmap="YlGnBu")
plt.show()

python 画图_第3张图片

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#plt.figure(figsize=(8,2))
## f1竖起来
f, ax = plt.subplots(figsize=(8, 2))
d ={'90%':[0.99],'80%':[0.99],'70%':[0.99],'60%':[0.99],'50%':[0.99],'40%':[0.99],'30%':[1],'20%':[1],'10%':[1],'0':[1]}
df = pd.DataFrame(d,index = ['f1'])
sns.heatmap(df,annot = True,cmap="YlGnBu",ax=ax)
## f1竖起来
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='right')
plt.show()

 

python 画图_第4张图片

5、画散点图,并多个图重合在一起

 

import matplotlib.pyplot as plt
import seaborn as sns
color = sns.color_palette()
sns.set_style('darkgrid')
%matplotlib inline
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号#有中文出现的情况,需要u'内容'

plt.figure(figsize=[9,6])
plt.plot(datat.index,datat_.t_bottom,color = 'r')
plt.plot(datat.index,datat_.t_top,color = 'c')
plt.scatter(datat.index,datat_.bottom,marker='.',c='g')
plt.scatter(datat.index,datat_.top,marker='.',c='b')
plt.legend(('t_bottom','t_top','noise_bottom','noise_top'),loc = 'upper right',fontsize=15)
plt.xlabel('index', fontsize=15)
plt.ylabel(u'温度', fontsize=15)
#plt.legend(['t_bottom','t_top'],loc = 'upper_right',fontsize = 10)
plt.show()

 

python 画图_第5张图片

6、画图颜色

color = 

python 画图_第6张图片

character color
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white

7、画靠在一起对比的柱状图

import matplotlib.pyplot as plt  
import seaborn as sns  
color = sns.color_palette()  
sns.set_style('darkgrid')  
%matplotlib inline  
n_groups = 2
MLKNN = (0.44, 0.66)
DT = (0.59,0.60)
RF = (0.61, 0.63)
MLP = (0.48,0.58)
 
# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.22
opacity = 0.8
 
rects1 = plt.bar(index, MLKNN, bar_width,
                 alpha=opacity,
                 label='MLKNN')
 
rects2 = plt.bar(index + bar_width, DT, bar_width,
                 alpha=opacity,
                 label='DT')

rects3 = plt.bar(index+ 2*bar_width, RF, bar_width,
                 alpha=opacity,
                 label='RF')
 
rects4 = plt.bar(index + 3*bar_width, MLP, bar_width,
                 alpha=opacity,
                 label='MLP')
 
plt.ylabel('F1')
plt.xticks(index + 1.5*bar_width, ('less_fea_add_noise', 'more_fea_add_noise'))
plt.title('add_noise')
plt.legend()
 
plt.tight_layout()
plt.show()

python 画图_第7张图片

# data to plot
fig, ax = plt.subplots()  
names = ('add_noise','denoise') 
models = ('KNN', 'DT', 'RF','MLP')
F1 = ((0.44,0.59,0.61,0.48),(0.66,0.60,0.63,0.58))


# 设置柱形图宽度
bar_width = 0.35
index = np.arange(len(F1[0]))
rects1 = plt.bar(index, F1[0], bar_width, label=names[0])
rects2 = plt.bar(index + bar_width, F1[1], bar_width, color='g', label=names[1])
plt.xticks(index + 0.5*bar_width, models)
plt.ylim(ymax = 0.8,ymin = 0)
def add_labels(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2.-0.1, 1.03*height, '%s' % float(height))
        
add_labels(rects1)
add_labels(rects2)
plt.legend()  
plt.tight_layout()  
plt.show()  

python 画图_第8张图片

8、统计数量的柱状图

 

### 统计用户信息:年龄、等级、性别比例
plt.figure(figsize=(12,4))
plt.subplot(131)
ax1 = sns.countplot(x="age", data=user)
plt.xlabel("Age")

plt.subplot(132)
ax2 = sns.countplot(x="sex", data=user)
plt.xlabel("sex")

plt.subplot(133)
ax3 = sns.countplot(x="user_lv_cd", data=user)
plt.xlabel("degree")
plt.show()

 

python 画图_第9张图片

9、画柱状图出错

sns.barplot 'list' object has no attribute 'dtype'

plt.figure(figsize=(10, 6))
cols = ['ip', 'app', 'device', 'os', 'channel']
uniques = [len(train[col].unique()) for col in cols]
####增加如下代码
cols = pd.Series(cols).unique()
uniques = pd.Series(uniques).unique()
#########
sns.set(font_scale=1.2)
ax = sns.barplot(cols, uniques, log=True)
ax.set(xlabel='Feature', ylabel='log(unique count)', title='Number of unique values per feature (from 10,000,000 samples)')
for p, uniq in zip(ax.patches, uniques):
    height = p.get_height()
    ax.text(p.get_x()+p.get_width()/2.,
            height + 10,
            uniq,
            ha="center") 
plt.show()

10、using ssh command in linux出现问题RuntimeError: Invalid DISPLAY variable

import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')  ##add this
 
xData = np.arange(0, 10, 1)
yData1 = xData.__pow__(2.0)
yData2 = np.arange(15, 61, 5)
plt.figure(num=1, figsize=(8, 6))
plt.title('Plot 1', size=14)
plt.xlabel('x-axis', size=14)
plt.ylabel('y-axis', size=14)
plt.plot(xData, yData1, color='b', linestyle='--', marker='o', label='y1 data')
plt.plot(xData, yData2, color='r', linestyle='-', label='y2 data')
plt.legend(loc='upper left')
plt.savefig('plot1.png', format='png')

11、折线图

click_count = []
num = []
for day in train.period.unique():
    click_count.append(train[train['period'] ==day]['click'].sum())
    num.append(train[train['period'] ==day].shape[0])
    
df = pd.DataFrame()
df['day'] = train.period.unique()
df['num'] = num
df['click_num'] = click_count
df = df.sort_values('day')
ax = sns.pointplot(x="day", y="num", data=df)
ax = sns.pointplot(x="day", y="click_num", data=df,color='g')

12、画多个柱状图

import matplotlib.pyplot as plt  
import seaborn as sns  
color = sns.color_palette()  
sns.set_style('darkgrid')  

%matplotlib inline 
plt.figure(figsize=(20,20))
for i in range(1,21):

    column = y_t.columns
    plt.subplot(5,4,i)
    ax1 = sns.countplot(x=column[i-1], data=y_t)
    plt.xlabel(column[i-1])

plt.show()

13、折线图

# -*- coding: utf-8 -*-

import warnings
warnings.filterwarnings('ignore')
 
import seaborn as sns
import matplotlib.pyplot as plt
 
import pandas as pd
import numpy as np
 
pd.options.display.max_columns  = 100

data = pd.read_excel("data.xlsx")[2:].reset_index(drop=True)
data.columns = ['数据集编号','线程数1','线程数2','线程数4','线程数8','线程数16','线程数32']
ss = data[['数据集编号']]
data = data[['线程数1','线程数2','线程数4','线程数8','线程数16','线程数32']]
data = data.T
data.columns = ss['数据集编号']

import matplotlib.pyplot as plt  
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = [0,1,2,3,4,5]

plt.plot(x,data['1_1'])
plt.plot(x,data['1_2'])
plt.plot(x,data['2_2'])
plt.plot(x,data['2_1'])
plt.plot(x,data['3_1'])
plt.plot(x,data['4_1'])
plt.plot(x,data['4_2'])
plt.plot(x,data['5_1'])
plt.plot(x,data['5_2'])

plt.legend(ss['数据集编号'], loc='upper left')
plt.xlabel('log(线程数)')
plt.ylabel('加速比')
plt.show()

python 画图_第10张图片

饼状图,而且figuresize没办法调整大小的话

import warnings
warnings.filterwarnings("ignore")
import pandas as pd
result = pd.value_counts(df_list)

import numpy as np    
import matplotlib
matplotlib.rcParams['figure.figsize'] = [10, 8]

import matplotlib.mlab as mlab    
import matplotlib.pyplot as plt
plt.figure(figsize=(6,9))
#根据value_counts()结果画饼图
df_dam = pd.DataFrame({'damage':result.index[0:],'fre':result.values[0:]})

plt.rc('font', family='SimHei', size=13)
fig = plt.figure()
plt.pie(df_dam.fre,
        labels=df_dam.damage,
        autopct='%1.2f%%',) #画饼图(数据,数据对应的标签,百分数保留两位小数点)
plt.title("damage ")
plt.show()

 python 画图_第11张图片

你可能感兴趣的:(python)