Matplotlip Bar Syntax.
import codecademylib
from matplotlib import pyplot as plt
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales1 = [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]
#Paste the x_values code here
n = 1 # This is our first dataset (out of 2)
t = 2 # Number of datasets
d = 6 # Number of sets of bars
w = 0.8 # Width of each bar
store1_x = [t*element + w*n for element
in range(d)]
plt.bar(store1_x,sales1)
n = 2 # This is our second dataset (out of 2)
t = 2 # Number of datasets
d = 6 # Number of sets of bars
w = 0.8 # Width of each bar
store2_x = [t*element + w*n for element
in range(d)]
plt.bar(store2_x,sales2)
plt.show()
Syntax for Stack Chart
plt.bar(range(len(drinks)), sales1)
plt.bar(range(len(drinks)), sales2, bottom=sales1)
plt.legend(["Location 1", "Location 2"])
plt.show()
Syntax to show error in bars -
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
ounces_of_milk = [6, 9, 4, 0, 9, 0]
error = [0.6, 0.9, 0.4, 0, 0.9, 0]
# Plot the bar graph here
plt.bar(range(len(drinks)),ounces_of_milk,yerr=error,capsize=5)
plt.show()
Syntax to use fill-in between:
plt.plot(months,revenue)
ax = plt.subplot()
ax.set_xticks(months)
ax.set_xticklabels(month_names)
y_lower = [0.9*i for i in revenue]
y_upper = [1.1*i for i in revenue]
plt.fill_between(months,y_lower,y_upper,alpha = 0.2)
plt.show()
months = x_value
Syntax to create a pie chart using legends and also display percentage to the nearest decimal using autopct
plt.pie(payment_method_freqs,autopct = '%0.1f%%')
plt.axis('equal')
plt.legend(payment_method_names)
Syntax to create a histogram and its various parameters for more visibility and understanding
use the keyword
alpha
, which can be a value between 0 and 1. This sets the transparency of the histogram. A value of 0 would make the bars entirely transparent. A value of 1 would make the bars completely opaque.use the keyword
histtype
with the argument 'step'
to draw just the outline of a histogram:To solve this, we can normalize our histograms using
normed=True
. This command divides the height of each column by a constant such that the total shaded area of the histogram sums to 1.plt.hist(sales_times1, bins=20,alpha=0.4,normed=True)
#plot your other histogram here
plt.hist(sales_times2,alpha=0.4,Normed=True)
plt.hist(a, range=(55, 75), bins=20, histtype='step')
plt.hist(b, range=(55, 75), bins=20, histtype='step')
We userange to set the range in between values in a histogram
Comments
Post a Comment