pasztora
7/29/2019 - 9:01 AM

Matplotlib visualization (basics)

# Basic lineplot using matplotlib
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
plt.plot(women_degrees['Year'], women_degrees['Biology']) # Defining x and y axis
plt.show()

# Lineplot with 2 data series
fig, ax = plt.subplots() # This is needed only when ".ax" is used
plt.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women', linewidth=3) # Setting color, label and line width of the line plot
plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men', linewidth=3)
plt.legend(loc='upper right')
plt.title('Percentage of Biology Degrees Awarded By Gender')
ax.tick_params(bottom="off", top="off", left="off", right="off") # Turning axes ticks off on the different sides of the chart
ax.set_xlim(1968, 2011) # Sets the limmit of the x-axis
ax.set_ylim(0, 100) # Sets the limit of the y-axis
plt.show()

# Labels can be added to the chart to replace legend
ax.text(2005, 62, "Men") # x, y coordinates and string
ax.text(2001, 35, "Women") # x, y coordinates and string for the 2nd dataseries

# Making a stacked barplot
df[["2FL [g/kg]", "DFL [g/kg]"]].plot.bar(stacked=True)