条形图带有使用子图的标准错误

问题描述 投票:-1回答:1

我有以下四个条形图,我想要的只是将它们放在子图中,如代码所示。谁能告诉我该怎么做?由于某种原因,它没有工作。下面是代码:(对不起,必须将其放在blockquote中,否则我可以发布问题。希望没关系)

编辑:为了澄清-我有四个单独的不同图表(下面的代码),我只想使用子图将它们放入一个图表中。但这由于某些原因不起作用。

import numpy as np 
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (12,5) 
Risk = ['3', '6', '9'] 
three = [0.0039942, 0.1278698, -0.1238756] 
six = [  0.0126427 , 0.2127346, -0.2000919] 
nine = [  0.0619075, 0.3083861, -0.2464786] 
twelve = [  0.1038604, 0.3864892 , -0.2826288]

error_three = [0.0105196, 0.0150707, 0.0102266]
error_six = [0.0147663, 0.0188184, 0.0112528] 
error_nine = [0.0193206, 0.0243935, 0.0145931] 
error_twelve = [ 0.0230019, 0.026358, 0.014386]


 ind = np.arange(len(Risk)) width = 0.23 fig, axs = plt.subplots()

 rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
 'K = 3',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="") rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="/")

 rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center', ecolor='black', capsize=5,color='white', edgecolor='black', hatch="-")

 rects4 = axs.bar(ind + width + width, twelve, width,
 yerr=error_twelve, label = 'K = 12',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="|")

 axs.set_xticks(ind) 
 axs.set_xticklabels(('Buy','Sell','Buy-sell'))
 axs.legend(loc=1, fontsize = 10) 
 axs.tick_params(labelsize=10)
 fig.suptitle('J = 3', fontsize=15)

代码块1

 three = [-0.0032653, 0.1075028, -0.1107681] 
 six = [  0.01766 , 0.2408147, -0.2231547] 
 nine = [  0.0611617, 0.3595795, -0.2984177] 
 twelve = [  0.1429058, 0.4146744 , -0.2717686]

 error_three = [0.0107142, 0.0161169, 0.0105163] 
 error_six = [0.0160058, 0.0220724,0.0119112] 
 error_nine = [0.020732, 0.0270203, 0.0131933]
 error_twelve = [ 0.0248679, 0.0311742, 0.0177063]

 ind = np.arange(len(Risk)) width = 0.23 fig, axs = plt.subplots()

 rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
 'K = 3',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="") rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="/")

 rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K
 = 9',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="-")

 rects4 = axs.bar(ind + width + width, twelve, width,
 yerr=error_twelve, label = 'K = 12',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="|")

 axs.set_xticks(ind) 
 axs.set_xticklabels(('Buy','Sell','Buy-sell'))
 axs.legend(loc=1, fontsize = 10) 
 axs.tick_params(labelsize=10)
 fig.suptitle('J = 6', fontsize=15)

代码块2

three = [0.0289784, 0.116908, -0.087973] 
six = [ 0.0918798 , 0.3250896, -0.2332098] 
nine = [  0.1791642, 0.3817135, -0.2025493] 
twelve = [ 0.2646989, 0.4489613 , -0.1842624]

 error_three = [0.0116038, 0.0168931, 0.0098965] 
 error_six = [0.0169266, 0.0231926,0.0109302] 
 error_nine = [0.021765, 0.0307299, 0.0155094]
 error_twelve = [ 0.0254275, 0.0328411,0.0152209]

 ind = np.arange(len(Risk)) width = 0.23 fig, axs = plt.subplots()

 rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
 'K = 3',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="") 
 rects2 = axs.bar(ind, six, width, 
                  yerr=error_six, label = 'K = 6',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="/")

 rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K
 = 9',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="-")

 rects4 = axs.bar(ind + width + width, twelve, width,
 yerr=error_twelve, label = 'K = 12',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="|")

 axs.set_xticks(ind) 
 axs.set_xticklabels(('Buy','Sell','Buy-sell'))
 axs.legend(loc=1, fontsize = 10) 
 axs.tick_params(labelsize=10)
 fig.suptitle('J = 9', fontsize=15)

代码块3

three = [0.0779324, 0.1986291, -0.1206967] 
six = [ 0.1796554,0.2999987, -0.1203433] 
nine = [  0.2796311, 0.3887926, -0.1091614]
twelve = [0.3887396, 0.4632978 , -0.0745582] 
error_three = [0.0122188,0.0175826, 0.0097949] 
error_six = [0.0169295, 0.0264755,0.0149082] 
error_nine = [0.0217114,0.0302063, 0.0130198] 
error_twelve = [0.0258077, 0.0332595,0.0135785]


 ind = np.arange(len(Risk)) width = 0.23 fig, axs = plt.subplots()

 rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
 'K = 3',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="") rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                 ecolor='black',
                 capsize=5,color='white',
               edgecolor='black',
                 hatch="/")

rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center',
                 ecolor='black',
                 capsize=5,color='white',
                 edgecolor='black',
                 hatch="-")

 rects4 = axs.bar(ind + width + width, twelve, width, yerr=error_twelve, label = 'K = 12',align='center', ecolor='black', capsize=5,color='white', edgecolor='black', hatch="|")

axs.set_xticks(ind) axs.set_xticklabels(('Buy','Sell','Buy-sell'))
axs.legend(loc=1, fontsize = 10) axs.tick_params(labelsize=10)
fig.suptitle('J = 12', fontsize=15)
fig, axs = plt.subplots(4, 1, constrained_layout=True)
plt.show()
python matplotlib
1个回答
1
投票

检查下面的代码,让我知道它是否正常运行。

import numpy as np 
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (12,5)

Risk = ['3', '6', '9'] 
three = [0.0039942, 0.1278698, -0.1238756] 
six = [  0.0126427 , 0.2127346, -0.2000919] 
nine = [  0.0619075, 0.3083861, -0.2464786] 
twelve = [  0.1038604, 0.3864892 , -0.2826288]

error_three = [0.0105196, 0.0150707, 0.0102266]
error_six = [0.0147663, 0.0188184, 0.0112528] 
error_nine = [0.0193206, 0.0243935, 0.0145931] 
error_twelve = [ 0.0230019, 0.026358, 0.014386]


ind = np.arange(len(Risk))
width = 0.23 
fig, axs = plt.subplots()

rects1 = axs.bar(ind - width, three, width, yerr=error_three, 
                label = 'K = 3',
                align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="")

rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="/")

rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center', ecolor='black', capsize=5,color='white', edgecolor='black', hatch="-")

rects4 = axs.bar(ind + width + width, twelve, width, yerr=error_twelve, label = 'K = 12',align='center',
                  ecolor='black',
                  capsize=5,color='white',
                  edgecolor='black',
                  hatch="|")

axs.set_xticks(ind) 
axs.set_xticklabels(('Buy','Sell','Buy-sell'))
axs.legend(loc=1, fontsize = 10) 
axs.tick_params(labelsize=10)
fig.suptitle('J = 3', fontsize=15)

# In[]: Code block 1

three = [-0.0032653, 0.1075028, -0.1107681] 
six = [  0.01766 , 0.2408147, -0.2231547] 
nine = [  0.0611617, 0.3595795, -0.2984177] 
twelve = [  0.1429058, 0.4146744 , -0.2717686]

error_three = [0.0107142, 0.0161169, 0.0105163] 
error_six = [0.0160058, 0.0220724,0.0119112] 
error_nine = [0.020732, 0.0270203, 0.0131933]
error_twelve = [ 0.0248679, 0.0311742, 0.0177063]

ind = np.arange(len(Risk))
width = 0.23 
fig, axs = plt.subplots()

rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
'K = 3',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="")

rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="/")

rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="-")

rects4 = axs.bar(ind + width + width, twelve, width,
yerr=error_twelve, label = 'K = 12',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="|")

axs.set_xticks(ind) 
axs.set_xticklabels(('Buy','Sell','Buy-sell'))
axs.legend(loc=1, fontsize = 10) 
axs.tick_params(labelsize=10)
fig.suptitle('J = 6', fontsize=15)

# In[]: Code block 2

three = [0.0289784, 0.116908, -0.087973] 
six = [ 0.0918798 , 0.3250896, -0.2332098] 
nine = [  0.1791642, 0.3817135, -0.2025493] 
twelve = [ 0.2646989, 0.4489613 , -0.1842624]

error_three = [0.0116038, 0.0168931, 0.0098965] 
error_six = [0.0169266, 0.0231926,0.0109302] 
error_nine = [0.021765, 0.0307299, 0.0155094]
error_twelve = [ 0.0254275, 0.0328411,0.0152209]

ind = np.arange(len(Risk)) 
width = 0.23 
fig, axs = plt.subplots()

rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
'K = 3',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="") 
rects2 = axs.bar(ind, six, width, 
                yerr=error_six, label = 'K = 6',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="/")

rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="-")

rects4 = axs.bar(ind + width + width, twelve, width,
yerr=error_twelve, label = 'K = 12',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="|")

axs.set_xticks(ind) 
axs.set_xticklabels(('Buy','Sell','Buy-sell'))
axs.legend(loc=1, fontsize = 10) 
axs.tick_params(labelsize=10)
fig.suptitle('J = 9', fontsize=15)

# In[]: Code block 3

three = [0.0779324, 0.1986291, -0.1206967] 
six = [ 0.1796554,0.2999987, -0.1203433] 
nine = [  0.2796311, 0.3887926, -0.1091614]
twelve = [0.3887396, 0.4632978 , -0.0745582] 
error_three = [0.0122188,0.0175826, 0.0097949] 
error_six = [0.0169295, 0.0264755,0.0149082] 
error_nine = [0.0217114,0.0302063, 0.0130198] 
error_twelve = [0.0258077, 0.0332595,0.0135785]


ind = np.arange(len(Risk)) 
width = 0.23 
fig, axs = plt.subplots()

rects1 = axs.bar(ind - width, three, width, yerr=error_three, label =
'K = 3',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="") 

rects2 = axs.bar(ind, six, width, yerr=error_six, label = 'K = 6',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="/")

rects3 = axs.bar(ind + width, nine, width, yerr=error_nine, label = 'K = 9',align='center',
                ecolor='black',
                capsize=5,color='white',
                edgecolor='black',
                hatch="-")

rects4 = axs.bar(ind + width + width, twelve, width, yerr=error_twelve, label = 'K = 12',align='center', ecolor='black', capsize=5,color='white', edgecolor='black', hatch="|")

axs.set_xticks(ind) 
axs.set_xticklabels(('Buy','Sell','Buy-sell'))
axs.legend(loc=1, fontsize = 10) 
axs.tick_params(labelsize=10)
fig.suptitle('J = 12', fontsize=15)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.