首页 > 解决方案 > Python Lollipop Plot,在线写标签

问题描述

我正在尝试复制此处的情节类型。

我有这个代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
# Create a dataframe
#value1=np.random.uniform(size=20)
#value2=value1+np.random.uniform(size=20)/4
#df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'value1':value1 , 'value2':value2 })
df = pd.read_csv('table_for_disease.txt', sep='\t')
print(df)
 

# Reorder it following the values of the first value:
ordered_df = df.sort_values(by='2000')
my_range=range(1,len(df.index)+1)
 
# The horizontal plot is made using the hline function
plt.hlines(y=my_range, xmin=ordered_df['2000'], xmax=ordered_df['2019'], color='grey', alpha=0.4)
plt.scatter(ordered_df['2000'], my_range, color='blue', alpha=1, label='2000')
plt.scatter(ordered_df['2019'], my_range, color='red', alpha=1 , label='2019')
plt.legend()
 
# Add title and axis names
plt.yticks(my_range, ordered_df['Disease'])
plt.title("Comparison of leading global causes of death in 2000 and 2019", loc='center')
plt.xlabel('Number of deaths (millions)')
plt.ylabel('Disease')

# Show the graph
plt.show()

输出是:

在此处输入图像描述

如您所见,图像非常拉伸。有人可以按照此处的原始示例演示如何在每行顶部编写 Y 标签。

标签: pythonmatplotlibplot

解决方案


推荐阅读