我需要准确地设置图表的维度。我试过了,但结果不是我所期望的(如果我设置了px和cm(。在上瘾中,我想知道如何正确导出图像。
import numpy as np
plt.rcParams['figure.dpi']=100
# create data
x = ['A', 'B', 'C', 'D']
y1 = np.array([10, 20, 10, 30])
y2 = np.array([20, 25, 15, 25])
y3 = np.array([12, 15, 19, 6])
y4 = np.array([10, 29, 13, 19])
# plot bars in stack manner
cm = 1/2.54 # centimeters in inches
px = 1/plt.rcParams['figure.dpi'] # pixel in inches
plt.figure(figsize=(800*px,1000*px))
plt.bar(x, y1, color='r')
plt.bar(x, y2, bottom=y1, color='b')
plt.bar(x, y3, bottom=y1+y2, color='y')
plt.bar(x, y4, bottom=y1+y2+y3, color='g')
plt.xlabel("Teams")
plt.ylabel("Score")
plt.legend(["Round 1", "Round 2", "Round 3", "Round 4"])
plt.title("Scores by Teams in 4 Rounds")
plt.show()
预期尺寸:800px x 1000 px,dpi=100我在这里附上一张导出图像的Photoshop截图尺寸不正确!
Figure构造函数接受一个默认值为80dpi的元组(以英寸为单位的数字(。您需要传递dpi参数来更改此
from matplotlib.figure import Figure
fig = Figure(figsize=(5, 4), dpi=80)
上面是5英寸乘4英寸在80dpi,这是400px乘320px
如果你想要800乘1000,你可以做
fig = Figure(figsize=(8, 10), dpi=100)
导出图像就像一样简单
fig.savefig("MatPlotLib_Graph.png", dpi = 100)