显示棒棒糖图表的值
show values of lollipop-chart
我是可视化数据的新手,我正在尝试制作棒棒糖图表,就像这张图表一样:
https://stats.stackexchange.com/questions/423735/what-is-the-name-of-this-plot-that-has-rows-with-two-connected-dots/423861#423861
但是我的注释失败了:
for row in df.itertuples():
ax.text(row.Index, y2_new+.5, s=round(y2_new, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
这是我的代码:
x3 = ["Alter: 0-29","Alter: 30-59","Alter: 60+"]
y3_new =[20, 44, 34]
y2_new =[13, 43, 43]
plt.hlines(y=x3, xmin = y3_new , xmax = y2_new, color='skyblue', alpha=0.4)
plt.scatter(y2_new, x3, color='#0096d7', s=50, label='Jazzmusiker', zorder=10)
plt.scatter(y3_new, x3, color='green', s=50 , label='Bluesmusiker', zorder=10)
plt.legend()
# Add title and axis names
plt.title("Sterbealter der Jazz- und Bluesmusiker", loc='left')
plt.xlabel('Value of the variables')
plt.ylabel('Alter')
# Annotate
for row in df.itertuples():
ax.text(row.Index, y2_new+.5, s=round(y2_new, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
# Show the graph
plt.show()
import matplotlib.pyplot as plt
x3 = ["Alter: 0-29","Alter: 30-59","Alter: 60+"]
y3_new =[20, 44, 34]
y2_new =[13, 43, 43]
plt.figure()
plt.hlines(y=x3, xmin = y3_new , xmax = y2_new, color='skyblue', alpha=0.4)
plt.scatter(y2_new, x3, color='#0096d7', s=50, label='Jazzmusiker', zorder=10)
plt.scatter(y3_new, x3, color='green', s=50 , label='Bluesmusiker', zorder=10)
plt.legend(loc="upper left")
# Add title and axis names
plt.title("Sterbealter der Jazz- und Bluesmusiker", loc='left')
plt.xlabel('Value of the variables')
plt.ylabel('Alter')
# Annotate
# y axis is categorical. Create a map between categories and Matplotlib
# numerical values on the y axis.
yticks_dict = {k: v for k, v in zip(x3, plt.yticks()[0])}
# use a small offset to place the annotation slightly above the
# categorical value
offset = 0.05
for x, y in zip(y2_new, x3):
plt.text(x, yticks_dict[y] + offset, round(x, 2), horizontalalignment='center', verticalalignment='bottom')
# set the ylim if necessary
new_offset = 5 * offset
plt.ylim(min(yticks_dict.values()) - new_offset, max(yticks_dict.values()) + new_offset)
# Show the graph
plt.show()
我是可视化数据的新手,我正在尝试制作棒棒糖图表,就像这张图表一样: https://stats.stackexchange.com/questions/423735/what-is-the-name-of-this-plot-that-has-rows-with-two-connected-dots/423861#423861
但是我的注释失败了:
for row in df.itertuples():
ax.text(row.Index, y2_new+.5, s=round(y2_new, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
这是我的代码:
x3 = ["Alter: 0-29","Alter: 30-59","Alter: 60+"]
y3_new =[20, 44, 34]
y2_new =[13, 43, 43]
plt.hlines(y=x3, xmin = y3_new , xmax = y2_new, color='skyblue', alpha=0.4)
plt.scatter(y2_new, x3, color='#0096d7', s=50, label='Jazzmusiker', zorder=10)
plt.scatter(y3_new, x3, color='green', s=50 , label='Bluesmusiker', zorder=10)
plt.legend()
# Add title and axis names
plt.title("Sterbealter der Jazz- und Bluesmusiker", loc='left')
plt.xlabel('Value of the variables')
plt.ylabel('Alter')
# Annotate
for row in df.itertuples():
ax.text(row.Index, y2_new+.5, s=round(y2_new, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
# Show the graph
plt.show()
import matplotlib.pyplot as plt
x3 = ["Alter: 0-29","Alter: 30-59","Alter: 60+"]
y3_new =[20, 44, 34]
y2_new =[13, 43, 43]
plt.figure()
plt.hlines(y=x3, xmin = y3_new , xmax = y2_new, color='skyblue', alpha=0.4)
plt.scatter(y2_new, x3, color='#0096d7', s=50, label='Jazzmusiker', zorder=10)
plt.scatter(y3_new, x3, color='green', s=50 , label='Bluesmusiker', zorder=10)
plt.legend(loc="upper left")
# Add title and axis names
plt.title("Sterbealter der Jazz- und Bluesmusiker", loc='left')
plt.xlabel('Value of the variables')
plt.ylabel('Alter')
# Annotate
# y axis is categorical. Create a map between categories and Matplotlib
# numerical values on the y axis.
yticks_dict = {k: v for k, v in zip(x3, plt.yticks()[0])}
# use a small offset to place the annotation slightly above the
# categorical value
offset = 0.05
for x, y in zip(y2_new, x3):
plt.text(x, yticks_dict[y] + offset, round(x, 2), horizontalalignment='center', verticalalignment='bottom')
# set the ylim if necessary
new_offset = 5 * offset
plt.ylim(min(yticks_dict.values()) - new_offset, max(yticks_dict.values()) + new_offset)
# Show the graph
plt.show()