matplotx轴文字排列方式

matplotx轴文字排列方式,第1张

Matplotlib可以通过设置tick_params来控制轴文字的排列方式,例如设置orientation属性值,可以控制轴文字的排列方式,如果设置为vertical表示垂直排列。此外,还可以使用labelpad属性来控制轴文字距离轴线的距离。

1, 导入matplotlib.pyplot, numpy 包:

import numpy as np

import matplotlib.pyplot as plt

2,添加主题样式:

plt.style.use('mystyle')

3, 设置图的大小,添加子图:

fig = plt.figure(figsize=(5,5))

ax = fig.add_subplot(111)

4,绘制sin, cos:

x = np.arange(-np.pi, np.pi, np.pi / 100)

y1 = np.sin(x)

y2 = np.cos(x)

sin, = ax.plot(x, y1, color='red', label='sin')

cos, = ax.plot(x, y2, color='blue', label='cos')

ax.set_ylim([-1.2, 1.2])

第二种方式 拆分显示

sin_legend = ax.legend(handles=[sin], loc='upper right')

ax.add_artist(sin_legend)

ax.legend(handles=[cos], loc='lower right')

plt.show()

import numpy as np

import matplotlib.pyplot as plt

# 添加主题样式

plt.style.use('mystyle')

# 设置图的大小,添加子图

fig = plt.figure(figsize=(5,5))

ax = fig.add_subplot(111)

for color in ['red', 'green']:

n = 750

x, y = np.random.rand(2, n)

scale = 200.0 * np.random.rand(n)

ax.scatter(x, y, c=color, s=scale,

label=color, alpha=0.3,

edgecolors='none')

ax.legend()

ax.grid(True)

plt.show()

为项目设置matplotlib参数

在代码执行过程中,有两种方式更改参数:

1,使用参数字典(rcParams);

2,调用matplotlib.rc()命令 通过传入关键字元祖,修改参数。

如果不想每次使用matplotlib时都在代码部分进行配置,可以修改matplotlib的文件参数。可以用matplot.get_config()命令来找到当前用户的配置文件目录。


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

原文地址: http://www.outofmemory.cn/tougao/11372517.html

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

发表评论

登录后才能评论

评论列表(0条)

保存