I'd like to make a horizontal bar chart with the length of the space mission (in years) on the x-axis plotted against cost of mission given on the y-axis.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('mission_lifetime.dat')
length_of_mission = data['Mission_End_Year']-data['Mission_Start_Year']
cost = data['Cost_in_millions']
plt.barh(length_of_mission, cost)
has the correct 'length' of the x-bar but not the correct offset for the start of the x-axis (in this case "data['Mission_Start_Year']")
Here is a sample of the data file:
Mission,Mission_Start_Year,Mission_End_Year,Cost_in_millions
ISO,1995,1998,615.
Herschel,2009,2013,1100.
Planck,2009,2013,700.
It is better to create the 'length' and 'cost' columns already in the data
DataFrame.
This worked for me:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('mission_lifetime.dat')
# Create column with mission length
data['Mission_Length']= data['Mission_End_Year']-data['Mission_Start_Year']
# Create column with colors
data['color'] = ['xkcd:pink',
'xkcd:baby blue',
'xkcd:mint']
# Basic plot, save Barcontainer in variable fig
fig = plt.barh(data['Cost_in_millions'],
data['Mission_Length'],
height=50,
left=data['Mission_Start_Year'],
color=data['color']
)
# Custom tickmarks
plt.xticks(np.arange(min(data['Mission_Start_Year']) - 5, max(data['Mission_Start_Year']) + 10, 3.0))
# Add labels inside
plt.bar_label(fig,
labels=data['Mission'],
label_type='center'
)
Output: