Adding a line to a bar chart - python

I want to create a bar line chart with matplotlib based on happiness index database.
I would like to indicate the generosity indexes of each of the countries on the secondary y-axis with a line chart. df1['Generosity']
fig, ax = plt.subplots()
ax.bar(df1['Country name'], df1['Ladder score'])
plt.xticks(rotation=30, ha='right')
fig.set_size_inches(18, 10, forward=True)

This solved my problem.
x=df1['Country name']
y=df1['Generosity']
ax2 = ax.twinx()
ax2 = plt.plot(x, y,color ='r')
plt.ylabel('Generosity')
plt.show()

Related

Reverse vertical one of the subplots in python

I have two data frames to plot. One of bar chart, another one is a line chart.
I want to plot the barchart upside to down. (vertically reversed.)
How can i handle this?
fig, ax1 = plt.subplots(figsize=(10, 5))
tidy = results.melt(id_vars='Day').rename(columns=str.title)
ax1 = sns.lineplot(x='Day', y='Value', hue='Variable', data=tidy, ax=ax1)
tidy2 = drugs.melt(id_vars='Day').rename(columns=str.title)
ax1 = sns.barplot(x='Day', y='Value', hue='Variable', data=tidy2, ax=ax1)
ax1.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax1.legend(loc=1)
ax1.tick_params(axis='x', labelrotation=45)

Trying to plot 2 charts side-by-side, but one of them always comes out empty

I have two plots that I generated from my data:
Here the second plot shows the distribution of results from the first one.
What I want is to plot them side-by-side so you could see both the data and the distribution on the same plot. And I want plots to share y-axis as well.
I tried to do the following:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(40, 15), sharey=True)
ax1 = sns.lineplot(plotting_df.index, plotting_df.error, color=('#e65400'), lw=2, label='random forest residual error')
ax1 = sns.lineplot(plotting_df.index, plotting_df.val, color=('#9b9b9b'), lw=1, label='current model residual error')
ax1 = sns.lineplot(plotting_df.index, 0, color=('#2293e3'), lw=1)
ax1.xaxis.set_visible(False)
ax1.set_ylabel('Residual Fe bias', fontsize=16)
ax1.set_title('Models residual error comparison', fontsize=20, fontweight='bold')
sns.despine(ax=ax1, top=True, bottom=True, right=True)
ax2 = sns.distplot(results_df.error, hist=True, color=('#e65400'), bins=81,
label='Random forest model', vertical=True)
ax2 = sns.distplot(plotting_df.val, hist=True, color=('#9b9b9b'),
bins=81, label='Rolling averages model', vertical=True)
ax2.set_title('Error distribution comparison between models', fontsize=20, fontweight='bold')
sns.despine(ax=ax2, top=True, right=True)
fig.savefig("blabla.png", format='png')
But when I do run it I get strange results - the first chart is in the second column, whereas I wanted it on the left and the second chart is completely blank. Not sure what I did wrong here.
Both lineplot and distplot accept a matplotlib axes object as an argument, which tells it which axes to plot onto. If no axes is passed into it, then the plot is placed onto the current axes.
You create a figure and 2 axes using :
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(40, 15), sharey=True)
Therefore, ax2 will be the current axes. So your distplot is being plotted on top of your lineplot, both in ax2.
You need to pass the axes into the seaborn plotting functions.
sns.lineplot(..., ax=ax1)
sns.distplot(..., ax=ax2)

Line plot over bar plot using Seaborn - Line plot won't render

I am trying to plot a line plot over a bar plot and cannot get the line plot to render with the bar chart. I can render the bar chart all the time and the line only when commenting out ax2. When I do render the line plot the dates show up in integer instead of date format. I think it has something to do with the X axis but cannot figure it out.
fig, ax = plt.subplots(figsize = (10, 10))
ax = sns.lineplot(x='Submission Date', y='Rating', data=df_cd)
ax2 = ax.twinx()
ax2 = sns.barplot(x='Submission Date', y='Count Handled', data=df_cd)
ax.set_xticklabels(ax.get_xticks())
plt.show()
The command plt.subplots(figsize = (10, 10)), indicates that you want to divide canvas and create the subplots on it.
For your current requirement, you could do something like -
ax = sns.barplot(x='Submission Date', y='Count Handled', data=df_cd)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), df_cd.Rating)
Here, you are creating the barplot first and adding the line plot over it with the same x-axis.

How to add more stocks to a graph with twin axis's?

I have this code below to plt trends of stocks, and have 2 axis's one axis on the left and the other on right of the chart for 2 stocks with different scales. I can't figure out how I can add more stocks to the graph. I just have 2 stocks, but I like to add more.
How can I modify my code to add more stocks to the second axis?
fig, ax1 = plt.subplots()
fig = plt.figure(figsize=(6,4))
t = newdf['date']
s1 = newdf['IBM']
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('Dates', fontsize=14)
ax1.set_xticklabels(t, rotation=45)
ax1.legend(loc=0)
ax1.grid()
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('Price', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
s2 = newdf['AAPL']
ax2.plot(t, s2, 'r-')
ax2.set_ylabel('Price', color='r')
ax2.tick_params('date', colors='r', rotation=90)
ax2.legend(loc=2)
fig.tight_layout()
plt.show()
[
add more stocks just above fig.tight_layout() like this.
For nth stock add ax(n),s(n) and add stock name chose color from below link.
ax3 = ax1.twinx()
s3 = newdf[stock name]
ax3.plot(t, s3, 'r-')
ax3.set_ylabel('Price', color=your color)
ax3.tick_params('date', colors=your color, rotation=90)
ax3.legend(loc=3)
Color
RGB

Matplotlib subplots: legend and axis-scale

I am plotting 4 subplots (i.e 2 rows 2 columns) in this way:
fig1= plt.figure(figsize=(8,6))
ax1 = fig1.add_subplot(221)
ax1.errorbar((r1),(xi1),fmt='',yerr=(low_err_1,upp_err_1),ls='none',color='black')
ax1.scatter((r1),(xi1),c='red',marker="o",s=30,label= r'$\xi(r)$ $0.0<z<0.5$')
ax1.plot((r1),(curve_y_1),'--',label='fit $0.0<z<0.5$')
ax1.set_xscale('log')
ax1.set_yscale('log')
ax2 = fig1.add_subplot(222)
ax2.errorbar((r2),(xi2),fmt='',yerr=(low_err_2,upp_err_2),ls='none',color='black')
ax2.scatter((r2),(xi2),c='blue',marker="o",s=30,label=r'$\xi(r)$ $0.5<z<1.0$')
ax2.plot((r2),(curve_y_2),'--',label='fit $0.5<z<1.0$')
ax2.set_xscale('log')
ax2.set_yscale('log')
ax3 = fig1.add_subplot(223)
ax3.errorbar((r3),(xi3),fmt='',yerr=(low_err_3,upp_err_3),ls='none',color='black')
ax3.scatter((r3),(xi3),c='yellow',marker="o",s=30,label=r'$\xi(r)$ $1.0<z<1.5$')
ax3.plot((r3),(curve_y_3),'--',label='fit $1.0<z<1.5$')
ax3.set_xscale('log')
ax3.set_yscale('log')
ax4 = fig1.add_subplot(224)
ax4.errorbar((r4),(xi4),fmt='',yerr=(low_err_4,upp_err_4),ls='none',color='black')
ax4.scatter((r4),(xi4),c='black',marker="o",s=30,label=r'$\xi(r)$ $1.5<z<2.0$')
ax4.plot((r4),(curve_y_4),'--',label='fit $1.5<z<2.0$')
ax4.set_xscale('log')
ax4.set_yscale('log')
My questions are:
Is there a way to add legends to all these subplots using a single (common) command, instead of typing ax1.legend(loc = 'best'), ax2.legend(loc = 'best') and so on separately for each subplot?
I would like to set log-scaling for each subplot using a single (common) command. As you can see, now I am setting the axis-scales separately to log for each subplot.
Just define a axes formatting function:
def style_ax(ax):
ax.legend(loc='best')
ax.set_yscale('log')
ax.set_xscale('log')
And than call it when finished:
for ax in [ax1, ax2, ax3, ax4]:
style_ax(ax)

Categories

Resources