I have a data frame with a related salary to the major.
I am trying to create horizontal bar charts of the majors sorted by salary.
My code looks like this:
fig, ax = plt.subplots()
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
ax.set_title('Majors by Salary')
ax.set_xlabel('Salary')
ax.set_ylabel('Majors')
However, my chart shows one emptly plots on top with title, x label and y label,
and then a horizontal barchart under the empty plots without title and labels.
Why is this happening?
Thanks for any help!
barh will plot in a new figure / axes by default.
Either you need to tell it to plot in the fig, ax you created before.
Or you can set title and labels in the active figure automatically created:
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
plt.title('Majors by Salary')
plt.xlabel('Salary')
plt.ylabel('Majors')
Related
I need to make the following chart: Number of Companies, Donations vs Year as a bar chart.
The following is my data:
Year = [2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
No_Companies = [123558,132335,147606,155790,161211,169784,174599,183888,198727,207317,217357,228996]
Donations=[144932,304607,642328,870509,1205382,1094624,2089240,2325322,2387036,3096069,4204255,3500766]
From what I have seen from other questions, most seem to have either their data in a dataframe or a list like [[x1,y1],[x2,y2]].
How can I get the chart I need from the data I have?
You can check this link out: Plot bar and line in same plot, different y-axes using matplotlib (no pandas)
The implementation can be done as follows:
plt.figure(1, figsize=(10,10))
barchart = plt.bar(Year, No_Companies, color='red')
plt.ylabel('No Companies')
plt.twinx()
barchart1 = plt.bar(Year, Donations, color='blue')
plt.ylabel('Donations')
Graph
I'm using Matplotlib to create 2 side-by-side horizontal bar charts showing regression coefficient importance across several words. I'd like to label the y-axes with each word in the list.
Every other word is appended to the y-axis when I try this:
# plot word importance bar graphs
fig, axes = plt.subplots(1,2,figsize=(5,10))
plt.subplots_adjust(wspace = 1)
axes[0].set_title('Low revenue')
axes[0].invert_yaxis()
axes[0].barh(np.arange(len(lowrev_topten)), lowrev_topten['Coefficient'])
axes[0].set_yticklabels(list(lowrev_topten['Word']))
axes[0].set_xlabel('Coefficient')
axes[1].set_title('High revenue')
axes[1].invert_yaxis()
axes[1].barh(np.arange(len(highrev_topten)), highrev_topten['Coefficient'])
axes[1].set_yticklabels(list(highrev_topten['Word']))
axes[1].set_xlabel('Coefficient')
However, when I remind it that I'd like to have 10 ticks for 10 words (plt.yticks(np.arange(0,10))), it fixes the second subplot:
# plot word importance bar graphs
fig, axes = plt.subplots(1,2,figsize=(5,10))
plt.subplots_adjust(wspace = 1)
plt.yticks(np.arange(0,10))
axes[0].set_title('Low revenue')
axes[0].invert_yaxis()
axes[0].barh(np.arange(len(lowrev_topten)), lowrev_topten['Coefficient'])
axes[0].set_yticklabels(list(lowrev_topten['Word']))
axes[0].set_xlabel('Coefficient')
axes[1].set_title('High revenue')
axes[1].invert_yaxis()
axes[1].barh(np.arange(len(highrev_topten)), highrev_topten['Coefficient'])
axes[1].set_yticklabels(list(highrev_topten['Word']))
axes[1].set_xlabel('Coefficient')
How do I get both subplots to have the proper y-tick labels?
Seems like you just need to set_yticks for each subplot.
fig, axes = plt.subplots(1,2,figsize=(5,10))
...
axes[0].set_yticks(np.arange(0,10))
axes[1].set_yticks(np.arange(0,10))
I have a dataframe with ~120 features that I would like to examine by year. I am plotting each feature, x = year, y = feature value within a loop. Whilst these plot successfully, the charts are illegible as they are totally squashed.
I have tried using plt.tight_layout() and adjusting the figure size using plt.rcParams['figure.figsize'] but sadly to no avail
for i in range(len(roll_df.columns)):
plt.subplot(len(roll_df.columns), 1, i+1)
name = roll_df.columns[i]
plt.plot(roll_df[name])
plt.title(name, y=0)
plt.yticks([])
plt.xticks([])
plt.tight_layout()
plt.show()
The loop runs but all plots are so squashed on the y-axis as to become illegible:
Matplotlib will not automatically adjust the size of your figure. So if you add more subplots below each other, it will split the available space instead of extending the figure. That's why your y axes are so narrow.
You could try to define the figure size beforehand, or determine the figure size based on how many subplots you have:
n_plots = roll_df.shape[1]
fig, axes = plt.subplots(n_plots, 1, figsize=(8, 4 * n_plots), tight_layout=True)
# Then your usual part, but plot on the created axes
for i in range(n_plots):
name = roll_df.columns[i]
axes[i].plot(roll_df[name])
axes[i].title(name, y=0)
axes[i].yticks([])
axes[i].xticks([])
plt.show()
I have plotted a simple heatmap with the below code in python. How would I go about adjusting the height/width of the individual heatmap cells with matplotlib?
def HeatMap(data):
# plot the figure
figure = plt.figure()
sub_figure = figure.add_subplot(111)
heatmap = sub_figure.imshow(data, interpolation='nearest',cmap='jet')
# add a color bar
cbar = figure.colorbar(ax=sub_figure, mappable=one_box, orientation='horizontal')
cbar.set_label('Scores')
plt.show()
I have tried figure = plt.figure(figsize=(10,10)) for example but it increases/decreases the whole figure rather than the individual cells. So far I have this:
I want the cells to be smaller in height so a bit squished.
I want to built a interactive scatter plot in python where i have a box, where i can click on another category and the plot is chaning. Right now I have this scatter plot for only one category (e.g. T-Shirts), based on the follinwg code:
METPrint = METView
fig, ax = plt.subplots()
colors = {'y':'red', 'n':'black'}
METPrint = METPrint[METPrint['Gender']=='Women']
METPrint = METPrint[METPrint['Class']=='T-Shirts']
ax.scatter(METPrint['EOH'], METPrint['SellOff'], c=METPrint['MarkdownFlag'].apply(lambda x: colors[x]))
plt.show()
Right now my rawdata looks like this:
Gender Class SellOff EOH MarkdownFlag
I want to have a filter i can click for the Class instead of writing 'T-Shirts' in the code above, can somehelp me out?
Thanks