1.Python:matplotlib绘图时指定图像大小,放大图像

matplotlib绘图时是默认的大小,有时候默认的大小会感觉图片里的内容都被压缩了,解决方法如下。
先是原始代码:

1

2

3

4

5

6

7

from matplotlib import pyplot as plt

 

plt.figure(figsize=(1,1)) #单位是英寸

 

= [1,2,3]

plt.plot(x, x)

plt.show()

  

关键的代码是plt.figure(figsize=(1,1)),生成的图片如下

Python:matplotlib绘图

修改代码,放大图片:

1

2

3

4

5

6

7

from matplotlib import pyplot as plt

 

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

 

= [1,2,3]

plt.plot(x, x)

plt.show()

  

这时候横坐标和纵坐标都放大了10倍:

Python:matplotlib绘图

如果想要指定像素,可以这么做:

1

2

3

4

5

6

7

from matplotlib import pyplot as plt

 

plt.figure(dpi=80)

 

= [1,2,3]

plt.plot(x, x)

plt.show()

  

Python:matplotlib绘图

 

2.Python:matplotlib绘图-scatter 散点图

https://matplotlib.org/gallery/lines_bars_and_markers/scatter_custom_symbol.html#sphx-glr-gallery-lines-bars-and-markers-scatter-custom-symbol-py

import matplotlib.pyplot as plt
import numpy as np

# unit area ellipse
rx, ry = 3., 1.
area = rx * ry * np.pi
theta = np.arange(0, 2 * np.pi + 0.01, 0.1)
verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])

x, y, s, c = np.random.rand(4, 30)
s *= 10**2.

fig, ax = plt.subplots()
ax.scatter(x, y, s, c, marker=verts)

plt.show()

 

Python:matplotlib绘图

相关文章: