Python:Barplot类似于嵌入式pictu

2024-09-29 06:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一篇论文中发现了一个很好的条形图,想做一些类似的东西,但不知道怎么做。我想做的数字如下

enter image description here 我做了以下可复制的例子:

import matplotlib.pyplot as plt
import numpy as np, pandas as pd, seaborn as sns;
data = np.array([[0.3,0.4,0.7],
                [0.5, 0.3,  0.8 ],
                [0.7,0.2,0.5],
                [0.2,0.4,0.1],
                [0.3,0.1, 0.8],
                [0.5,0.9, 0.2],
                [1.4,1.7,1.2],
                [0.4,0.8, 0.3],
                [1.8,2.6, 0.4],
                [2.7,3.7, 2.1],
                [0.1,4.1, 2.4]])
data_df =  pd.DataFrame(data, columns=['SMB', 'HML', 'MOM'], index = ['OLS3', 'EN', 'GLM', 'NN1','NN2','NN3','NN4','NN5','NN6', 'NN7','NN7'])

然后我试着创建这样的条形图:

data_df.plot(kind='bar',figsize=(16, 10))

但这不是我想要的。有人知道如何制作与我添加的图片相似的图片吗?你知道吗


Tags: importnumpydfdatamatplotlibasnp图片
1条回答
网友
1楼 · 发布于 2024-09-29 06:27:49
import matplotlib.pyplot as plt
import numpy as np, pandas as pd, seaborn as sns;
data = np.array([[0.3,0.4,0.7],
                [0.5, 0.3,  0.8 ],
                [0.7,0.2,0.5],
                [0.2,0.4,0.1],
                [0.3,0.1, 0.8],
                [0.5,0.9, 0.2],
                [1.4,1.7,1.2],
                [0.4,0.8, 0.3],
                [1.8,2.6, 0.4],
                [2.7,3.7, 2.1],
                [0.1,4.1, 2.4]])
data_df =  pd.DataFrame(data, columns=['SMB', 'HML', 'MOM'], index = ['OLS3', 'EN', 'GLM', 'NN1','NN2','NN3','NN4','NN5','NN6', 'NN7','NN7'])
ax=data_df.plot(kind='bar',figsize=(16, 10),fontsize=15)
plt.xticks(rotation=-45)
#set ylim
plt.ylim(-0.5, 4.5)
#grid on
plt.grid()
# set y=0
ax.axhline(0, color='black', lw=1)
#change size of legend
ax.legend(fontsize=20)
#hiding upper and right axis layout
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
#changing the thickness
ax.spines['bottom'].set_linewidth(3)
ax.spines['left'].set_linewidth(3)

输出:

enter image description here

相关问题 更多 >