Matplotlib-- graphics perfect modified plt.legend, plt.gca, set_major_formatter

Legend comment plt.legend ()

plt.legend ( LOC = '', title =, fontSize = 12 is, frameon = True, the fancybox = True, framealpha = 0.2, borderpad = 0.3, ncol =. 1, markerfirst = True, markerscale =. 1, bbox_to_anchor =, =. 1 NumPoints , = 3.5 of handlelength, Shadow =)
parameter meaning:
** LOC: ** legend position ( 'best' = 0, ' upper right' = 1, 'upper left' = 2, 'lower left' = 3, 'lower right '= 4,' right '= 5,' center left '= 6,' center right '= 7,' lower center '= 8,' upper center '= 9,' center '= 10), using bbox_to_anchor, the the invalid
frameon: whether to display the legend border
fontsize: font size
ncol: the number of columns legend, the default is 1
Shadow:Whether to add the legend is dark borders
fancybox: whether to round the corners of the legend box
fancybox: there are four parameters, the abscissa is the first representative of a pattern width of
several times
, and the second represents the ordinate, the same token, the first the length and width of the third and fourth frame represented
Here Insert Picture Description

Canvas parameters

plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

** num: ** image number or name
** figsize: ** figure specified width and height in inches;
dpi : drawing object parameter specifies the resolution
** facecolor: ** Background color
** edgecolor: ** border color
** frameon: ** whether to show border

fig=plt.figure()
ax=fig.add_axes([a,b,c,d])

Parameter Meaning fig.add_axes of:
moving an image (a, b) units
** a: ** distance from the leftmost
** b: ** distance from the lowermost
** c, d: ** in the lower left corner as a starting point the length and width of the canvas

plt.title(sting,family,size,color,style,loc)

** string: ** Title Content
** family: ** Font categories
** size: ** Font Size
** color: ** Font Size
** style: ** Style
** loc: ** Location

The abscissa data modification

Attention to the need to be as abscissa label use pd.to_datetime conversion index

import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import matplotlib as mpl
os.chdir(r'C:\Users\MAR\Desktop\新建文件夹')

#读取数据
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
print(data)
print(data.dtypes)

#!!!!必须将数据转换成日期型,否则出现没有日期数据无法转换
#data['顺序']=pd.to_datetime(data['顺序'])#和下面一句功能一样
data.顺序=pd.to_datetime(data.顺序)

#计算出按照‘顺序’作为索引的平均值
data1=data.groupby(by='顺序').mean()
print(data1)
print(data1.dtypes)

#解决中文乱码问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

#获取当前坐标
ax=plt.gca()
data_format=mpl.dates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(data_format)
# # 设置多少标签
xlocator= mpl.ticker.LinearLocator(12)
# # 自动设置主要标签
ax.xaxis.set_major_locator(xlocator)
plt.plot(data1.index,data1.1,linestyle='-',linewidth=3,color='blue')
plt.xticks(rotation= -45)

plt.show()

Here Insert Picture Description

Add text to graphics

import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import matplotlib as mpl
os.chdir(r'C:\Users\MAR\Desktop\新建文件夹')

#读取数据,不修改
data=pd.read_csv(r'my_csv_date.csv',encoding='gbk')
print(data)
#将数据重新设置索引分组
plt.bar(x=data.index.values,height=data.1,linestyle='-',linewidth=3,color='lightblue',\
         tick_label=data.顺序)
plt.xticks(data.index,rotation= -45)
#未对信息处理
plt.show()

#对信息求和处理
data=data.groupby(by='顺序').sum()
print(data)
plt.bar(x=data.index.values,height=data.1,linestyle='-',linewidth=3,color='lightblue',\
         tick_label=data.index)
plt.xticks(data.index,rotation= -45)
#无指向型注释文本
plt.text(-1,6,'最大属性',fontsize=15,color='red')
#有指向型,xy表示箭头位置,xytext表示文本开始位置,arrow是字典类型的数据
plt.annotate('最大位置',xy=(2,20),xytext=(4,17),color='orange',fontsize=15,\
            arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='red'))
plt.show()

Position of the text can be an arrow in FIG method intersection line shown in green , the data processing after resetting the index
Here Insert Picture Description

Published 70 original articles · won praise 1 · views 2413

Guess you like

Origin blog.csdn.net/weixin_43794311/article/details/105118999