Python数据科学库Numpy、Pandas、Matplotlib和Seaborn在Jupyter Notebook中的一些全局功能设置

Python数据科学库Numpy、Pandas、Matplotlib和Seaborn在Jupyter Notebook中的一些全局功能设置,第1张

文章目录
  • 一、导入Python数据科学库
  • 二、Numpy库的一些全局功能设置
  • 三、Pandas库的一些全局功能设置
  • 四、Matplotlib库的一些全局功能设置
  • 五、Seaborn库的一些全局功能设置

一、导入Python数据科学库
from warnings import filterwarnings
filterwarnings('ignore')   # 过滤警告信息

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rcParams
from pandas import DataFrame, Series
二、Numpy库的一些全局功能设置
np.set_printoptions(threshold = np.inf)   # 解决numpy array print后不能完全显示
np.set_printoptions(suppress = True)      # 若想不以科学计数显示
三、Pandas库的一些全局功能设置

1、恢复默认设置

pd.reset_option('all')                       # 重置所有设置,恢复默认
pd.reset_option('display.max_rows')          # 重置设置,恢复默认

2、行设置

# None可以写具体的数字,写多少就显示多少
pd.set_option('display.max_rows', None)      # 显示所有行
pd.options.display.max_rows = None           # 显示所有行(两种方法都可以)

pd.set_option('display.max_rows', 200)       # 最多显示200行
pd.options.display.max_rows = 200	      	 # 最多显示200行(两种方法都可以)

pd.set_option('display.min_rows', 10)        # 最少显示10行
pd.options.display.min_rows = 10             # 最少显示10行(两种方法都可以)

3、列设置

# None可以写具体的数字,写多少就显示多少
pd.set_option('display.max_columns', None)   # 显示所有列
pd.options.display.max_columns = None        # 显示所有列(两种方法都可以)

4、单独元素显示宽度设置

# 显示DataFrame列中单独元素的最大宽度,None可以写具体的数字,写多少就显示多少
pd.set_option('display.max_colwidth', None) 

# 设置DataFrame每列的最大显示宽度为100,即字符数(两种方法都可以)
pd.set_option('display.max_colwidth', 100) 
pd.options.display.max_colwidth = 100

5、小数精度设置

# 设置float列的显示精度,让其只显示2位(这个设置不影响底层数据,它只影响显示)
pd.set_option( 'display.precision', 2)
pd.options.display.precision = 2

# 设置Dataframe显示到小数点后2位
pd.set_option('display.float_format', lambda x: '%.2f' % x)   
四、Matplotlib库的一些全局功能设置

1、内置魔法函数,不用再 plt.show()

%matplotlib inline 

2、全局配置

# 设置全局字体为Times New Roman(但是中文会显示不出来 !)
plt.rc('font', family='Times New Roman')   

config = {
    "font.family":'serif',        # 衬线字体
    "font.size": 12,              # 相当于小四大小
    "mathtext.fontset":'stix',    # matplotlib 渲染数学字体时使用的字体,和 Times New Roman 差别不大
    "font.serif": ['SimSun'],     # 宋体 SimSun
    "axes.unicode_minus": False,  # 用来正常显示负号
    "xtick.direction":'in',       # 横坐标轴的刻度向内(in)或向外(out)
    "ytick.direction":'in',       # 纵坐标轴的刻度向内(in)或向外(out)
    "xtick.labelsize": 12,        # 横轴字体大小
    "ytick.labelsize": 12,        # 纵轴字体大小
}
rcParams.update(config)

3、坐标轴和标题设置

# 设置坐标轴刻度范围
plt.xlim((-5, 5))
plt.ylim((-2, 2))

# 设置坐标轴名称、字体、大小
plt.xlabel('xxxx', fontdict={'family':'Times New Roman', 'size':16})
plt.ylabel('yyyy', fontdict={'family':'Times New Roman', 'size':16})
plt.title('ttttt', fontdict={'family':'Times New Roman', 'size':16})

# 设置坐标轴刻度、字体、大小
plt.xticks(np.arange(-5, 5, 0.5), fontproperties = 'Times New Roman', size = 10)
plt.yticks(np.arange(-2, 2, 0.3), fontproperties = 'Times New Roman', size = 10)

4、图大小和图例设置

plt.figure(figsize=(10,6))

# 图例字体、大小
plt.legend(prop={'family':'Times New Roman', 'size':16})

5、图保存设置

# 要在 plt.show() 前 save
plt.savefig(r'图像.png', dpi=600, bbox_inches='tight')

6、中文宋体,数字和英文 Times New Roman 绘图案例

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

# 设置全局字体及大小,设置公式字体
config = {
    "font.family":'serif',        # 衬线字体
    "font.size": 12,              # 相当于小四大小
    "mathtext.fontset":'stix',    # matplotlib渲染数学字体时使用的字体,和Times New Roman差别不大
    "font.serif": ['SimSun'],     # 宋体SimSun
    "axes.unicode_minus": False,  # 用来正常显示负号
    "xtick.direction":'in',       # 横坐标轴的刻度设置向内(in)或向外(out)
    "ytick.direction":'in',       # 纵坐标轴的刻度设置向内(in)或向外(out)
}
rcParams.update(config)

plt.figure(figsize=(10,6))
x = np.linspace(0, 3*np.pi, 100)
plt.plot(x, np.sin(1*x), label=u"正弦$\mathrm{y=sin(x)}$")   # 图例:中文宋体,数字和英文Times New Roman
plt.plot(x, np.sin(2*x), label="正弦图像")                   # 图例:中文宋体
plt.plot(x, np.sin(3*x), label=u"$\mathrm{y=sin(3x)}$")     # 图例:数字和英文Times New Roman
plt.legend(prop={'family': 'SimSun', 'size': 12})           # 设置图例字体为宋体

plt.title(u'正弦函数 $\mathrm{y \; = \; sin(x)}$', size=16)        # 图名:正弦函数 y = sin(x)
plt.xlabel('横坐标', size=14)                                     # 横轴标签:横坐标
plt.ylabel('Values', fontproperties='Times New Roman', size=14)  # 纵轴标签:Values

# 设置横纵坐标刻度值为'Times New Roman'
plt.xticks(fontproperties='Times New Roman', size=12)
plt.yticks(fontproperties='Times New Roman', size=12)

plt.savefig(r'D:\Users\Administrator\Desktop\正弦图像.png', dpi=600, bbox_inches='tight')
plt.show()

五、Seaborn库的一些全局功能设置

1、恢复默认设置

sns.set()   # 重置Seaborn默认值

2、绘图设置

sns.set_style(style="darkgrid")                          # 配置样式
sns.set_context(context="poster",font_scale=1.5)         # 配置字体
sns.set_palette(sns.color_palette("RdBu", n_colors=7))   # 配置色板

3、中文乱码解决

from matplotlib.font_manager import FontProperties

myfont=FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf', size=14)
sns.set(font=myfont.get_name())

边学边记录,未完……

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/792261.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-05
下一篇2022-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存