I am producing a plot and I want to make the secondary y-axes 10 times smaller than the main y-axes.
ax2 = ax.twinx()
sns.lineplot(data=df["A"], ax=ax)
sns.lineplot(data=df["B"], ax=ax2)
is it possible to do define ax2 = ax.twinx()/10? how can specifcy that ax2 should be 10 times lower than ax? for example if ax goes from 0 to 100, ax2 should go from 0 to 1.
IIUC, use get_ylim and set_ylim to re-adjust the limit of ax2 :
ax2 = ax.twinx()
sns.lineplot(data=df["A"], ax=ax)
sns.lineplot(data=df["B"], ax=ax2)
ax2.set_ylim(0, ax.get_ylim()[1]/100) # <- add this line
NB : If you need a view limit from 0 to 1, you need to divide by 100 and not 10.
Output :
Related
Below code is to create two parameter subplots of data per pdf page and I'm using the 'axhline' function to plot the data limits of each parameter. However when I use it inside the for loop, only the second subplot had the data limits - the first one always doesn't show. I suppose what happens is axhline works one at a time in the for loop? Instead of having it in both subplots by end of the for loop? Please have a look in my code below:
fig, ax = plt.subplots(2, 1)
ax = ax.flatten()
for i, col in enumerate(hib_list[0:2]):
ax1 = sns.boxplot(x=lotid, y=cc_df[col], ax=ax[i], hue=temp)
ax1.set_ylabel(col,fontsize=8)
ax1.get_legend().remove()
plt.tight_layout(pad=1.0)
#Below line is to set data limits
plt.axhline(y=500, color='r', linestyle='--')
pdf.savefig()
plt.close()
Below is a corrected version of your code. You can set once per loop ax=axes[i] and then use ax for all operations. plt.(…) works only on the last plotted axes object.
fig, axes = plt.subplots(2, 1)
axes = axes.flatten()
for i, col in enumerate(hib_list[0:2]):
ax = axes[i]
sns.boxplot(x=lotid, y=cc_df[col], ax=ax, hue=temp)
ax.set_ylabel(col,fontsize=8)
ax.get_legend().remove()
#Below line is to set data limits
ax.axhline(y=500, color='r', linestyle='--')
plt.tight_layout(pad=1.0)
pdf.savefig()
plt.close()
I'm new to python and attempting to chart some time series data. I'm using pyplot to create 3 stacked line charts which have the same x-axis (dates), but a different scale for the y-axes. However, each y-axis, as well as the x-axis for the bottom chart, have overlapping labels. There are labels generated from 0 to 1, as well as axis labels from my data set. How do I turn 'off' the auto-generated 0 to 1 labels on the y-axes and the bottom x-axis?
fig, ax = plt.subplots(3,1,sharex='all', squeeze=False, figsize=(12,8))
ax = fig.add_subplot(3,1,1)
plt.plot(df1['date'], df1['value'])
ax2 = fig.add_subplot(3,1,2)
plt.plot(df2['date'], df2['value'])
ax3 = fig.add_subplot(3,1,3)
plt.plot(df3['date'], df3['value'])
plt.show()
You can see the issue in the below picture. Any help is greatly appreciated!
You have already created subplots with all the axes in the initial assignment
fig, ax = plt.subplots(3,1,sharex='all', squeeze=False, figsize=(12,8))
therefore the following assignements of
ax = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2)
ax3 = fig.add_subplot(3,1,3)
are not only unnecessary, but they seem to overlap the already created subplots (if you change it to add_subplot(2,1,1) you will notice it just starts dividing figure again and overlaying axes on top of each other).
What you want to do, is access the axes created in plt.subplots() call:
fig, ax = plt.subplots(3,1,sharex='all', squeeze=False, figsize=(12,8))
ax[0].plot(df1['date'], df1['value'])
ax[1].plot(df2['date'], df2['value'])
ax[2].plot(df3['date'], df3['value'])
plt.show()
Simulated Output:
Data from seaborn tips dataset
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
Let's say I want to plot four sets of axes on the same plot that all share from each other as follows:
ax2 shares a y-axis with ax1. Therefore, I do ax2 = ax1.twiny()
ax3 shares a x-axis with ax1. So, I do ax3 = ax1.twinx()
Now, ax4 needs to share its x-axis with ax2, but its y-axis with ax3. I know that if it shared both axes with, say, ax2 I could do ax4 = ax2.twinx().twiny(), but what do I do where I need it to share each with a different axis?
I am trying to get the values of xticks from one plot and then use these values for another plot but set the new ticks as 10 to the power of the other plot's ticks. The following lines doesn't do the job I am aiming for
labels=[item for item in ax1.get_xticklabels()]
ax2.set_xticklabels(['$10^{{{:d}}}$'.format(int(i)) for i in labels])
I will appreciate for any suggestion.
What about sharing axes ? This will fix the same limits and number of ticks for ax1 and ax2 :
fig, ax = plt.subplots(1, 2, sharex=True)
ax1 = ax[0]
ax2 = ax[1]
Then your code will do the trick since you are sure that both subplots have the same xticks
labels = [item for item in ax2.get_xticklabels()]
ax2.set_xticklabels(['$10^{{{:d}}}$'.format(int(i)) for i in labels])