将百分比添加到每个组的子组 likert-scale Python
Adding percentages to subgroups of each group likert-scale Python
我想知道是否可以使用 MatPlotLib 在李克特量表的条形图中包含每个子组的百分比细分?任何帮助将不胜感激!
Python代码:
from numpy.core import numeric
import plot_likert
import pandas as pd
# define my selections
myscale1 = \
['Very unlikely',
'Unlikely',
'Neutral',
'Likely',
'Very likely']
# create a likert plot
ax1 = plot_likert.plot_likert(GenZ, myscale1, plot_percentage=True, figsize=(15,15), colors=plot_likert.colors.likert5)
ax1.set_title(('Casually watch big matches'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})
ax1.tick_params(axis='y', labelsize=20)
ax1.legend(loc="upper middle", ncol=5)
下面的示例代码假定您使用的是 nmalkin/plot-likert。它创建水平条。有6组钢筋。首先,一个虚拟的不可见组来创建正确的间距。然后一组每个比例(每组有一个固定的颜色)。
您可以使用 matplotlib 的新功能 bar_label
来标记条形(bar_label
是 matplotlib 3.4 之后的新功能,因此可能需要升级):
import plot_likert
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
rng = np.random.default_rng(seed=42)
data = pd.DataFrame(rng.choice(plot_likert.scales.agree, (200, 2)), columns=['Q1', 'Q2'])
ax = plot_likert.plot_likert(data, plot_likert.scales.agree, plot_percentage=True, figsize=(14, 4))
for bars, color in zip(ax.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
ax.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)
plt.tight_layout()
plt.show()
@JohanC 回答了我的问题-
您需要更新到最新的 MatPlotLib
import plot_likert
import pandas as pd
# define my selections
myscale1 = \
['Strongly disagree',
'Somewhat disagree',
'Neither agree nor disagree',
'Somewhat agree',
'Strongly agree']
# create a likert plot
ax1 = plot_likert.plot_likert(agree_statements, myscale1, plot_percentage=True, figsize=(20,20), colors=plot_likert.colors.likert5)
ax1.set_title(('Casually watch big matches (21-34 year old)'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})
ax1.set_xlabel('% Breakdown',fontdict={'fontsize':28})
ax1.tick_params(axis='y', labelsize= 15)
ax1.tick_params(axis='x', labelsize= 10)
for bars, color in zip(ax1.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
ax1.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)
我想知道是否可以使用 MatPlotLib 在李克特量表的条形图中包含每个子组的百分比细分?任何帮助将不胜感激!
Python代码:
from numpy.core import numeric
import plot_likert
import pandas as pd
# define my selections
myscale1 = \
['Very unlikely',
'Unlikely',
'Neutral',
'Likely',
'Very likely']
# create a likert plot
ax1 = plot_likert.plot_likert(GenZ, myscale1, plot_percentage=True, figsize=(15,15), colors=plot_likert.colors.likert5)
ax1.set_title(('Casually watch big matches'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})
ax1.tick_params(axis='y', labelsize=20)
ax1.legend(loc="upper middle", ncol=5)
下面的示例代码假定您使用的是 nmalkin/plot-likert。它创建水平条。有6组钢筋。首先,一个虚拟的不可见组来创建正确的间距。然后一组每个比例(每组有一个固定的颜色)。
您可以使用 matplotlib 的新功能 bar_label
来标记条形(bar_label
是 matplotlib 3.4 之后的新功能,因此可能需要升级):
import plot_likert
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
rng = np.random.default_rng(seed=42)
data = pd.DataFrame(rng.choice(plot_likert.scales.agree, (200, 2)), columns=['Q1', 'Q2'])
ax = plot_likert.plot_likert(data, plot_likert.scales.agree, plot_percentage=True, figsize=(14, 4))
for bars, color in zip(ax.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
ax.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)
plt.tight_layout()
plt.show()
@JohanC 回答了我的问题-
您需要更新到最新的 MatPlotLib
import plot_likert
import pandas as pd
# define my selections
myscale1 = \
['Strongly disagree',
'Somewhat disagree',
'Neither agree nor disagree',
'Somewhat agree',
'Strongly agree']
# create a likert plot
ax1 = plot_likert.plot_likert(agree_statements, myscale1, plot_percentage=True, figsize=(20,20), colors=plot_likert.colors.likert5)
ax1.set_title(('Casually watch big matches (21-34 year old)'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})
ax1.set_xlabel('% Breakdown',fontdict={'fontsize':28})
ax1.tick_params(axis='y', labelsize= 15)
ax1.tick_params(axis='x', labelsize= 10)
for bars, color in zip(ax1.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
ax1.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)