Matplotlib (drawing and visualization)

1 Introduction

  Matplotlib Python is a powerful graphics and data visualization toolkit. Data visualization is one of the most important tasks of our data analysis can help us to complete many operations, complete the data analysis of the final result is to be an interactive data visualization.

  Installation: pip install matplotlib

  Reference method: import matplotlib.pyplot as plt

2.plot function (line graph)

Import numpy AS NP 
 Import PANDAS AS PD 
 Import matplotlib.pyplot AS PLT 

plt.rcParams [ ' font.sans serif- ' ] = [ ' SimHei ' ]    # is limited to the window system, which join two, added solve Chinese 
plt.rcParams [ ' axes.unicode_minus ' ] = False 

X = [2,9,5,10 ] 
Y = [1,2,3,4 ] 

PLT. Figure (figsize = (10,6))    # set canvas size 

plt.title ( ' title title ' , fontSize = 20 is, Color = ' Red ' )  # Header size and color 
plt.xlabel ( ' OK ' , 20 is fontSize =)    # Set value x 
plt.ylabel ( ' column ' , 20 is fontSize =)      # Set the value of y 


plt.plot (x, y, marker = ' O ' , lineStyle = ' - ' )   # drawing, marker = 'o' is displayed at the turning point circle 
# lineStyle dotted line represents 
plt.show () # display screen

3.bar function (histogram)

pd.read_csv = DF ( ' ./douban_movie.csv ' )   # the incoming data into Excel inside 
df.head ()   # View first five data 

RES = df.groupby ( ' origin ' ) .size (). sort_values (Ascending False =)   # according to the 'origin' movie division, ort_index () index sorting installation, sort_values () values are sorted installed 
RES 

X = res.index 
Y = res.values 

PLT. Figure (figsize = (20,6))    # set canvas size 
plt.title ( ' number in each country or region's cinema ' , fontSize = 20)   # title size and color 
plt.xticks (rotation = 90, fontSize = 20, color = 'red')    # rotation=90 翻转90度
plt.xlabel('产地',fontsize=20)

plt.yticks(fontsize=20)
plt.ylabel('数量',fontsize=20)

for a,b in zip(x,y):
    plt.text(a, b+150 ,b, horizontalalignment='center',fontsize=15)
    
plt.bar(x,y)
plt.show()

 

 4.pie function (pie chart)

df.head()
df_res = df['时长']
df_res

res = pd.cut(df_res,[0, 60, 90, 120,140,1000])  # df_res是待分割的源数据 [0, 60, 90, 120,140,1000] 是区间,左开右闭
res

res = res.value_counts()
res

x = res.index
y = res.values
plt.title('电影时长分布图',fontsize=20)

patch, l_text, p_text = plt.pie(y, labels = x, autopct='%.2f%%')

for p in p_text:
    p.set_size(12)
    p.set_color('white')
    
for l in l_text:
    p.set_size(13)
    p.set_color('red')

plt.pie(y)
plt.show()

5、保存图表到文件

  常用格式:plt.savafig('文件名.拓展名')

  文件类型是通过文件扩展名推断出来的。因此,如果你使用的是.pdf,就会得到一个PDF文件。

plt.savefig('123.pdf')

  savefig并非一定要写入磁盘,也可以写入任何文件型的对象,比如BytesIO:

from io import BytesIO
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
 
参数 说明
fname 含有文件路径的字符串或者Python的文件型对象。
dpi 图像分辨率,默认为100
format 显示设置文件格式("png","jpg","pdf","svg","ps",...)
facecolor 背景色,默认为"W"(白色)
bbox_inches 图表需要保存的部分。设置为”tight“,则尝试剪除图表周围空白部分

Guess you like

Origin www.cnblogs.com/blue-tea/p/11985349.html