如何将 TreeMap 和 Pie Chart 添加为子图?
How to add TreeMap and Pie Chart as Subplot?
我正在尝试添加 PIE 图表和 Treemap 作为子图。
如下所示(预期):-
根据 squarify documentation,我试图将轴对象作为 ax 参数传递。但是它不起作用。在传递轴对象时,第二个图是空的。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
import squarify
# Fixing random state for reproducibility
np.random.seed(19680801)
dt = 0.01
t = np.arange(0, 10, dt)
nse = np.random.randn(len(t))
r = np.exp(-t / 0.05)
cnse = np.convolve(nse, r) * dt
cnse = cnse[:len(t)]
s = 0.1 * np.sin(2 * np.pi * t) + cnse
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
fig.set_figheight(7)
fig.set_figwidth(13)
#plt.subplot(211)
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = ['Frogs', 'Hogs']
sizes = [15, 30]
explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs')
ax0.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
#plt.subplot(212)
#ax1.psd(s, 512, 1 / dt)
plt.show()
volume = [350, 220, 170, 150, 50]
labels = ['Liquid\n volume: 350k', 'Savoury\n volume: 220k', 'Sugar\n volume: 170k',
'Frozen\n volume: 150k', 'Non-food\n volume: 50k']
color_list = ['#0f7216', '#b2790c', '#ffe9a3', '#f9d4d4', '#d35158', '#ea3033']
plt.rc('font', size=14)
squarify.plot(sizes=volume, label=labels,
color=color_list, alpha=0.7)
plt.axis('off')
plt.show()
我找到了解决方案。上面的代码几乎是正确的,除了 1 个问题。在 Squerify 的情节之前,有一个调用 show 方法的语句。删除其工作后。
信用:@JahanC
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
import squarify
# Fixing random state for reproducibility
np.random.seed(19680801)
dt = 0.01
t = np.arange(0, 10, dt)
nse = np.random.randn(len(t))
r = np.exp(-t / 0.05)
cnse = np.convolve(nse, r) * dt
cnse = cnse[:len(t)]
s = 0.1 * np.sin(2 * np.pi * t) + cnse
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
fig.set_figheight(7)
fig.set_figwidth(13)
#plt.subplot(211)
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = ['Frogs', 'Hogs']
sizes = [15, 30]
explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs')
ax0.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
#plt.subplot(212)
#ax1.psd(s, 512, 1 / dt)
volume = [350, 220, 170, 150, 50]
labels = ['Liquid\n volume: 350k', 'Savoury\n volume: 220k', 'Sugar\n volume: 170k',
'Frozen\n volume: 150k', 'Non-food\n volume: 50k']
color_list = ['#0f7216', '#b2790c', '#ffe9a3', '#f9d4d4', '#d35158', '#ea3033']
plt.rc('font', size=14)
squarify.plot(sizes=volume, label=labels, ax=ax1,
color=color_list, alpha=0.7)
plt.axis('off')
plt.show()
我正在尝试添加 PIE 图表和 Treemap 作为子图。
如下所示(预期):-
根据 squarify documentation,我试图将轴对象作为 ax 参数传递。但是它不起作用。在传递轴对象时,第二个图是空的。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
import squarify
# Fixing random state for reproducibility
np.random.seed(19680801)
dt = 0.01
t = np.arange(0, 10, dt)
nse = np.random.randn(len(t))
r = np.exp(-t / 0.05)
cnse = np.convolve(nse, r) * dt
cnse = cnse[:len(t)]
s = 0.1 * np.sin(2 * np.pi * t) + cnse
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
fig.set_figheight(7)
fig.set_figwidth(13)
#plt.subplot(211)
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = ['Frogs', 'Hogs']
sizes = [15, 30]
explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs')
ax0.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
#plt.subplot(212)
#ax1.psd(s, 512, 1 / dt)
plt.show()
volume = [350, 220, 170, 150, 50]
labels = ['Liquid\n volume: 350k', 'Savoury\n volume: 220k', 'Sugar\n volume: 170k',
'Frozen\n volume: 150k', 'Non-food\n volume: 50k']
color_list = ['#0f7216', '#b2790c', '#ffe9a3', '#f9d4d4', '#d35158', '#ea3033']
plt.rc('font', size=14)
squarify.plot(sizes=volume, label=labels,
color=color_list, alpha=0.7)
plt.axis('off')
plt.show()
我找到了解决方案。上面的代码几乎是正确的,除了 1 个问题。在 Squerify 的情节之前,有一个调用 show 方法的语句。删除其工作后。 信用:@JahanC
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.gridspec as gridspec
import squarify
# Fixing random state for reproducibility
np.random.seed(19680801)
dt = 0.01
t = np.arange(0, 10, dt)
nse = np.random.randn(len(t))
r = np.exp(-t / 0.05)
cnse = np.convolve(nse, r) * dt
cnse = cnse[:len(t)]
s = 0.1 * np.sin(2 * np.pi * t) + cnse
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
fig.set_figheight(7)
fig.set_figwidth(13)
#plt.subplot(211)
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = ['Frogs', 'Hogs']
sizes = [15, 30]
explode = (0, 0.1) # only "explode" the 2nd slice (i.e. 'Hogs')
ax0.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
#plt.subplot(212)
#ax1.psd(s, 512, 1 / dt)
volume = [350, 220, 170, 150, 50]
labels = ['Liquid\n volume: 350k', 'Savoury\n volume: 220k', 'Sugar\n volume: 170k',
'Frozen\n volume: 150k', 'Non-food\n volume: 50k']
color_list = ['#0f7216', '#b2790c', '#ffe9a3', '#f9d4d4', '#d35158', '#ea3033']
plt.rc('font', size=14)
squarify.plot(sizes=volume, label=labels, ax=ax1,
color=color_list, alpha=0.7)
plt.axis('off')
plt.show()