Reputation: 91
I have a dateframe called for_plot
and I am using the following snippet of code to make a bar diagram:
import matlplotlib.pyplot as plt
f, ax = plt.subplots(1,1,figsize=(10,8))
x_1 = for_plot['res_code'] - 0.2
x_2 = for_plot['res_code']
pre = for_plot['n_indnota_pre']
post = for_plot['n_indnota_post']
ax.bar(x_1,pre, width = 0.2, color = 'red')
ax.bar(x_2,post, width = 0.2, color = 'green')
ax.set_xticks([0.9,1.9,2.9])
ax.set_xticklabels(['GEN','ST','SC'])
ax.legend(['pre_nota', 'post_nota'],loc = 1)
ax.xlabel('Constituency Type')
ax.ylabel('No of Independent Candidates')
ax.title('Average No Of Independent Candidates by Constituency Type')
plt.show()
I do understand how to work around with matplotlib but I have a few questions on the nuances:
f
in line 1 of the snippet do? As the snippet stands, what do f
and ax
represent? ax.xlabel('Constituency Type')
but while drawing other figures, If I don't call subplot()
in the first line and use plt.xlabel('Constituency Type')
, it works absolutely fine. Why does it behave this way?I get the following error:
AttributeError: 'AxesSubplot' object has no attribute 'xlabel'
Edit 1:
f.xlabel('Constituency Type')
f.ylabel('No of Independent Candidates')
f.title('Average No Of Independent Candidates by Constituency Type')
Doesn't work either.
AttributeError:'Figure' object has no attribute 'xlabel'
Upvotes: 1
Views: 2912
Reputation: 770
Hope below answer will help.
f
represent Figure
object which is the top level container for all the plot elements and ax
is object or array of Axes
objects (in your case its single object)
You can use code below for setting label and title of subplots:
ax.set_xlabel('common xlabel')
ax.set_ylabel('common ylabel')
ax.set_title('ax title')
Upvotes: 2