gpt4 book ai didi

python - Sankey 与 Matplotlib 标签位置

转载 作者:行者123 更新时间:2023-12-01 06:53:33 25 4
gpt4 key购买 nike

在此之后: Sankey with Matplotlib我编写了这段代码:

from matplotlib.sankey import Sankey
import matplotlib.pyplot as plt
from matplotlib import rcParams


## Plotten
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{underscore}')
plt.rc('font', family='serif')
plt.rcParams['font.size'] = 18
plt.rcParams['font.serif'] = "Linux Libertine"

fig = plt.figure(figsize = [10,10], dpi=330)
ax = fig.add_subplot(1, 1, 1,)

Sankey(ax=ax, flows = [ 20400,3000,-19900,-400,-2300,-800],
labels = ['Value K', 'Value S', 'Value H', 'Value F', 'Value Sp', 'uncertainty'],
orientations = [ 1, 1, -1, -1, 0, 1 ],
scale=1/40000, trunklength=1, unit = 'kg', pathlengths=0.5, gap=.5,
edgecolor = '#000000', facecolor = 'grey'
).finish()
plt.axis("off")
plt.show()

结果是:enter image description here

如何将标签(或值)移出图表?

有没有办法获得“.”在值中作为千位分隔符,例如 20.400kg?

最佳答案

我明白了

from matplotlib.sankey import Sankey
import matplotlib.pyplot as plt
from matplotlib import rcParams

plt.rc('font', family = 'serif')
plt.rcParams['font.size'] = 10
plt.rcParams['font.serif'] = "Linux Libertine"

fig = plt.figure(figsize = [6,4], dpi=330)
ax = fig.add_subplot(1, 1, 1,)
sankey = Sankey(ax=ax, scale=1/40000, unit = 'kg',gap=.9,)

sankey.add(flows = [ 20400,3000,-19900,-400,-2300,-800],
labels = ['Value K', 'Value S', 'Value H', 'Value F', 'Value Sp', 'uncertainty'],
orientations = [ 1, 1, -1, -1, 0, 1 ],
trunklength = 1, pathlengths = 0.5, edgecolor = '#000000', facecolor = 'grey')
diagrams = sankey.finish()

print(diagrams[0].texts[0])
print(diagrams[0].texts[1])
print(diagrams[0].texts[2])
print(diagrams[0].texts[3])
print(diagrams[0].texts[4])
print(diagrams[0].texts[5])

diagrams[0].texts[0].set_position(xy=[-0.756, 1]) # K
diagrams[0].texts[1].set_position(xy=[-1.955, 1]) # S
diagrams[0].texts[2].set_position(xy=[ 1.6625, -1.2]) # H
diagrams[0].texts[3].set_position(xy=[ 0.506, -1]) # F
diagrams[0].texts[4].set_position(xy=[ 1.5, 0.45]) # Sp
diagrams[0].texts[5].set_position(xy=[ 0.5, 1]) # U

plt.axis("off")
plt.show()

enter image description here

关于python - Sankey 与 Matplotlib 标签位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900854/

25 4 0