带有 Matplotlib 标签位置的 Sankey
Sankey with Matplotlib Positions of Labels
在此之后:
我做了这个代码:
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()
结果如下:
如何将标签(或值)移出图表?
有没有办法得到一个“.”在值中作为千位分隔符,例如 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()
在此之后:
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()
结果如下:
如何将标签(或值)移出图表?
有没有办法得到一个“.”在值中作为千位分隔符,例如 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()