0

I'm trying to replicate the type of plot here.

I have this code:

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()

The output is:

enter image description here

As you can see the image is very stretched. Could someone demonstrate how to write the Y labels on top each of the lines, as per the original example here.

Slowat_Kela
  • 1,193
  • 2
  • 15
  • 32

0 Answers0