Having python plot two different x-axes with different scales - python

Here's my code so far:
QE_ellip_fixed = [-1.04e-3,-1.04e-2,-0.1,-0.76,-2.34,-2.54]
QL_ellip_fixed = [1.77e-4,9.89e-4,-6e-2,-2.9,-4.45,-2.74]
QP_ellip_fixed = [1.26e-3,1.45e-2,0.14,0.98,2.6,2.5]
QE_ellip_varied = [-1.73e-4,-1.73e-3,-1.71e-2,-0.15,-0.86,-3.16]
QL_ellip_varied = [7.57e-5,7.53e-4,5.4e-3,-0.13,-4.15,-7.3]
QP_ellip_varied = [1.41e-3,1.77e-3,2.34e-2,0.22,1.33,3.14]
RHScalls_ellip = [764021,76388,7625,750,63,3]
RHScalls_circ = [629171,62864,6234,577,41,5]
QE_circ_fixed= [-1.26e-4,-1.26e-3,-1.24e-2,-0.11,-0.57,-2.98]
QL_circ_fixed = [-1.32e-4,5.89e-4,1.5e-3,-0.51,0.4,-9.57]
QP_circ_fixed = [1.45e-2,9.25e-3,4.62e-2,0.58,3.5,8.54]
QE_circ_varied = [-1.26e-4,-1.25e-3,-1.24e-2,-0.11,-0.56,-2.13]
QL_circ_varied = [-1.33e-4,5.88e-4,1.69e-3,-0.45,-0.64,-6.58]
QP_circ_varied = [1.45e-2,9.32e-3,5.2e-2,0.55,3.11,13.05]
alp = [1e-5,1e-4,1e-3,1e-2,1e-1,1]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(alp,np.abs(QE_ellip_varied),label='$|Q_E|$')
ax1.plot(alp,np.abs(QL_ellip_varied),label='$|Q_L|$')
ax1.plot(alp,np.abs(QP_ellip_varied),label='$|Q_P|$')
ax2 = ax1.twiny()
ax2.set_xticks([1e-5,1e-4,1e-3,1e-2,1e-1,1])
ax2.set_xticklabels(RHScalls_ellip)
ax1.set_xscale('log')
plt.yscale('log')
ax1.grid()
ax1.set_xlabel('alpha')
ax1.set_ylabel('Score (unitless)')
ax1.legend()
plt.show()
And here's the outputted image:
I want to have the values on the top axis have ticklabels in line with the grid lines already imposed, as they actually correspond to those gridlines, but I can't seem to make the top axis not behave in this annoying logarithmic way. I've only specified for axis 1 to have a logarithmic scale, but it seems to have applied to axis 2 as well...

it's because the axes share the y-axis, but not the x-axis. The xlimits are different for each..the following worked for me:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax1.set_xscale('log')
ax2.set_xscale('log') #make sure both log
plt.yscale('log')
ax1.plot(alp,np.abs(QE_ellip_varied),label='$|Q_E|$')
ax1.plot(alp,np.abs(QL_ellip_varied),label='$|Q_L|$')
ax1.plot(alp,np.abs(QP_ellip_varied),label='$|Q_P|$')
ax2.set_xlim(ax1.get_xlim()) #make sure same limits
ax2.set_xticks([1e-5,1e-4,1e-3,1e-2,1e-1,1])
ax2.set_xticklabels(RHScalls_ellip)
ax1.grid()
ax1.set_xlabel('alpha')
ax1.set_ylabel('Score (unitless)')
ax1.legend()

Related

Ticks and Labels on Twin Axes

How can I set the labels on the extra axes?
The ticks and labels should be the same on all 4 axes. I'm doing something wrong... Thanks!
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
plt.show()
# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]
# Version 1: twinx() + xaxis.set_ticks()
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)
ax2.xaxis.set_ticks(ticks, labels=labels)
ax3.yaxis.set_ticks(ticks, labels=labels)
plt.show()
# Version 2: twinx() + set_xticklabels)()
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax1.set_xticks(ticks)
ax1.set_xticklabels(labels)
ax1.set_yticks(ticks)
ax1.set_yticklabels(labels)
ax2.set_xticks(ticks)
ax2.set_xticklabels(labels)
ax3.set_yticks(ticks)
ax3.set_yticklabels(labels)
plt.show()
Confused: How come ax1 has both xaxis and yaxis, while ax2, ax3 do not appear to?
A unintuitive solution based on matplotlib.axes.Axes.twinx:
Create a new Axes with an invisible x-axis and an independent y-axis
positioned opposite to the original one (i.e. at right).
This means unintuitively (at least for me) you have to switch x/y at the .twin call.
unintuitively not concerning the general matplotlib twinx functionality, but concerning such a manual ticks and label assignment
To highlight that a bit more I used ax2_x and ax3_y in the code.
Disclaimer: Not sure if that will break your plot intention when data is added.
Probably at least you have to take special care with the data assignment to those twin axes - keeping that "axis switch" in mind.
Also keep that axis switch" in mind when assigning different ticks and labels to the x/y axis.
But for now I think that's the plot you were looking for:
Code:
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)
ax2_x = ax1.twiny() # switch
ax3_y = ax1.twinx() # switch
ax2_x.xaxis.set_ticks(ticks, labels=labels)
ax3_y.yaxis.set_ticks(ticks, labels=labels)
plt.show()
Or switch the x/yaxis.set_ticks - with the same effect:
On second thought, I assume that's the preferred way to do it, especially when data comes into play.
ax2_x = ax1.twinx()
ax3_y = ax1.twiny()
ax2_x.yaxis.set_ticks(ticks, labels=labels) # switch
ax3_y.xaxis.set_ticks(ticks, labels=labels) # switch
In case you don't intend to use the twin axis functionality (that means having different data with different scaling assigned to those axis) but 'only' want the ticks and labels on all 4 axis for better plot readability:
Solution based on answer of ImportanceOfBeingErnest with the same plot result:
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
# create reusable ticks and labels
ticks = [0,1/2,3.14159/4,3.14159/2,1]
labels = [r"$0$", r"$\displaystyle\frac{1}{2}$", r"$\displaystyle\frac{\pi}{4}$", r"$\displaystyle\frac{\pi}{2}$", r"$1$"]
plt.figure(figsize=(5,5))
f, ax1 = plt.subplots()
ax1.xaxis.set_ticks(ticks, labels=labels)
ax1.yaxis.set_ticks(ticks, labels=labels)
ax1.tick_params(axis="x", bottom=True, top=True, labelbottom=True, labeltop=True)
ax1.tick_params(axis="y", left=True, right=True, labelleft=True, labelright=True)
plt.show()
ax2 = ax1.twinx() shares the x-axis with ax1.
ax3 = ax1.twiny() shares the y-axis with ax1.
As a result, the two lines where you set ax2.xaxis and ax3.yaxis's ticks and ticklabels are redundant with the changes you already applied on ax1.
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = False # My computer doesn't have LaTeX, don't mind me.
# Create reusable ticks and labels.
ticks = [0, 1/2, 3.14159/4, 3.14159/2, 1]
labels = [r"$0$", r"$\frac{1}{2}$", r"$\frac{\pi}{4}$", r"$\frac{\pi}{2}$", r"$1$"]
# Set the ticks and ticklabels for each axis.
fig = plt.figure(figsize=(5,5))
ax1 = fig.add_subplot()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
for axis in (ax1.xaxis,
ax1.yaxis,
ax2.yaxis,
ax3.xaxis):
axis.set_ticks(ticks)
axis.set_ticklabels(labels)
fig.show()
Notice that if I comment out the work on ax2 and ax3, we get exactly what you have in your question:
for axis in (ax1.xaxis, ax1.yaxis,
# ax2.yaxis,
# ax3.xaxis,
):
axis.set_ticks(ticks)
axis.set_ticklabels(labels)
Now let's ruin ax1 via modifications on ax2, just to show that the bound between twins works well:
ax2.xaxis.set_ticks(range(10))
ax2.xaxis.set_ticklabels(tuple("abcdefghij"))

Creating a 2x2 subplot from one dataset as different graphs

I have a large census dataset I am working with and am taking different data from it and representing it as a singular .png in the end. I have created the graphs individually, but when I try to add them to the subplots they get distorted or axis get messed up.
Current code:
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
ax1.pie(df.data.valuecounts(normalize=True),labels=None,startangle-240)
ax1.legend(['a','b','c','d','e'])
ax1.axis('equal')
data2=df[['A']].dropna().values
kde=df.A.plot.kde()
binss = np.logspace(0.01,7.0)
ax2=plt.hist(hincp, normed=True, bins=binss)
ax2=plt.xscale('log')
ax3 = df.replace(np.nan,0)
ax3 = (df.groupby(['G'])['R'].sum()/1000)
ax3.plot.bar(width=0.9, color='red',title='Gs').set_ylabel('Rs')
ax3.set_ylabel('Rs')
ax3.set_xlabel('# G')
t = df[['p','o','s','y']]
ax4=plt.scatter(t.o,t.p,s=t.s,c=t.y, marker = 'o', alpha = 0.2)
plt.ylim(0, 10000)
plt.xlim(0,1200000)
cbar=plt.colorbar()
plt.title("this vs that", loc = 'center')
plt.xlabel('this')
plt.ylabel('that')
All four types of graphs should be displayed and not overlap.
You create Axes for each subplot but then you don't use them.
ax1.pie(...) looks correct but later you don't use ax2,ax3,ax4.
If you are going to to use the DataFrame plotting methods, just call plt.subplot before each new plot. Like this.
df = pd.DataFrame(np.random.random((6,3)))
plt.subplot(3,1,1)
df.loc[:,0].plot()
plt.subplot(3,1,2)
df.loc[:,1].plot()
plt.subplot(3,1,3)
df.loc[:,2].plot()
plt.show()
plt.close()
Or use the Axes that you create.
df = pd.DataFrame(np.random.random((6,3)))
fig = plt.figure()
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2)
ax3 = fig.add_subplot(3,1,3)
ax1.plot(df.loc[:,0])
ax2.plot(df.loc[:,1])
ax3.plot(df.loc[:,2])
plt.show()
plt.close()

How do I create a figure of these two separate polar plots?

I'm attempting to create two separate plots as subplots, on the same figure. Both plots are polar. My attempts cause them to plot on the same graph.
def GenerateTrigonometryTable(x): #Define Function
A = np.arange (0,360,x)
B = np.sin(A*np.pi/180)
C = np.cos(A*np.pi/180)
table = np.dstack(([A],[B],[C]))
return table
Theta = (GenerateTrigonometryTable(5)[:,:,0])
STheta = (GenerateTrigonometryTable(5)[:,:,1])
CTheta = (GenerateTrigonometryTable(5)[:,:,2])
ax1 = plt.subplot(111, projection='polar')
ax1.plot(Theta.flatten(), STheta.flatten())
ax2 = plt.subplot(111, projection='polar')
ax2.plot(Theta.flatten(), CTheta.flatten())
fig.show()
This plots it on the same graph and I need it to be a figure of two separate graphs.
You need the following: 121 means first plot on a 1x2 subplots grid and 122 means second plot on that 1x2 subplots grid.
ax1 = plt.subplot(121, projection='polar')
ax1.plot(Theta.flatten(), STheta.flatten())
ax2 = plt.subplot(122, projection='polar')
ax2.plot(Theta.flatten(), CTheta.flatten())
fig.show()
A more object-oriented approach would be :
fig = plt.figure()
ax1 = fig.add_subplot(121, projection='polar')
ax2 = fig.add_subplot(122, projection='polar')
ax1.plot(Theta.flatten(), STheta.flatten())
ax2.plot(Theta.flatten(), CTheta.flatten())
fig.show()
Equivalent of Sheldore's answer but shows how figures, axes and plots are articulated in matplotlib.

Two Y axis Bar plot: custom xticks

I am trying to add custom xticks to a relatively complicated bar graph plot and I am stuck.
I am plotting from two data frames, merged_90 and merged_15:
merged_15
Volume y_err_x Area_2D y_err_y
TripDate
2015-09-22 1663.016032 199.507503 1581.591701 163.473202
merged_90
Volume y_err_x Area_2D y_err_y
TripDate
1990-06-10 1096.530711 197.377497 1531.651913 205.197493
I want to create a bar graph with two axes (i.e. Area_2D and Volume) where the Area_2D and Volume bars are grouped based on their respective data frame. An example script would look like:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy
fig = plt.figure()
ax1 = fig.add_subplot(111)
merged_90.Volume.plot(ax=ax1, color='orange', kind='bar',position=2.5, yerr=merged_90['y_err_x'] ,use_index=False , width=0.1)
merged_15.Volume.plot(ax=ax1, color='red', kind='bar',position=0.9, yerr=merged_15['y_err_x'] ,use_index=False, width=0.1)
ax2 = ax1.twinx()
merged_90.Area_2D.plot(ax=ax2,color='green', kind='bar',position=3.5, yerr=merged_90['y_err_y'],use_index=False, width=0.1)
merged_15.Area_2D.plot(ax=ax2,color='blue', kind='bar',position=0, yerr=merged_15['y_err_y'],use_index=False, width=0.1)
ax1.set_xlim(-0.5,0.2)
x = scipy.arange(1)
ax2.set_xticks(x)
ax2.set_xticklabels(['2015'])
plt.tight_layout()
plt.show()
The resulting plot is:
One would think I could change:
x = scipy.arange(1)
ax2.set_xticks(x)
ax2.set_xticklabels(['2015'])
to
x = scipy.arange(2)
ax2.set_xticks(x)
ax2.set_xticklabels(['1990','2015'])
but that results in:
I would like to see the ticks ordered in chronological order (i.e. 1990,2015)
Thanks!
Have you considered dropping the second axis and plotting them as follows:
ind = np.array([0,0.3])
width = 0.1
fig, ax = plt.subplots()
Rects1 = ax.bar(ind, [merged_90.Volume.values, merged_15.Volume.values], color=['orange', 'red'] ,width=width)
Rects2 = ax.bar(ind + width, [merged_90.Area_2D.values, merged_15.Area_2D.values], color=['green', 'blue'] ,width=width)
ax.set_xticks([.1,.4])
ax.set_xticklabels(('1990','2015'))
This produces:
I omitted the error and colors but you can easily add them. That would produce a readable graph given your test data. As you mentioned in comments you would still rather have two axes, presumably for different data with proper scales. To do this you could do:
fig = plt.figure()
ax1 = fig.add_subplot(111)
merged_90.Volume.plot(ax=ax, color='orange', kind='bar',position=2.5, use_index=False , width=0.1)
merged_15.Volume.plot(ax=ax, color='red', kind='bar',position=1.0, use_index=False, width=0.1)
ax2 = ax1.twinx()
merged_90.Area_2D.plot(ax=ax,color='green', kind='bar',position=3.5,use_index=False, width=0.1)
merged_15.Area_2D.plot(ax=ax,color='blue', kind='bar',position=0,use_index=False, width=0.1)
ax1.set_xlim([-.45, .2])
ax2.set_xlim(-.45, .2])
ax1.set_xticks([-.35, 0])
ax1.set_xticklabels([1990, 2015])
This produces:
Your problem was with resetting just one axis limit and not the other, they are created as twins but do not necessarily follow the changes made to one another.

How do I increase the spacing between subplots with subplot2grid?

I use subplot2grid to make a subplot like the following:
In order to make the ticks sufficiently large for publication, I need to increase the vertical and horizontal spacing between axes.
Normally, I would use something like subplot_adjust(hspace = 0.5), but that doesn't seem to work with subplot2grid.
Could anyone please recommend a solution?
Here is the code I use to plot things and create the axes:
import matplotlib.pyplot as plt
ax1 = plt.subplot2grid((2,2),(0,0), colspan = 2)
ax2 = plt.subplot2grid((2,2),(1,0), colspan = 1)
ax3 = plt.subplot2grid((2,2),(1,1), colspan = 1)
df.plot( ax = ax1)
plt.show()
I've found the solution here
The code is as follows:
AX = gridspec.GridSpec(2,2)
AX.update(wspace = 0.5, hspace = 0.5)
ax1 = plt.subplot(AX[0,:])
ax2 = plt.subplot(AX[1,0])
ax3 = plt.subplot(AX[1,1])
Which produces the same subplots with increased horizontal and vertical spacing.
You can add the following line:
plt.subplots_adjust(hspace=0.8)
under this one:
ax3 =plt.subplot2grid((2,2),(1,1), colspan = 1)
You can play with all sorts of parameters this way.

Categories