The tracker in the lower-right corner (highlighted in red) reports y-values relative to the y-axis on the right.
How can I get the tracker to report y-values relative to the y-axis on the left instead?
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(6)
numdata = 100
t = np.linspace(0.05, 0.11, numdata)
y1 = np.cumsum(np.random.random(numdata) - 0.5) * 40000
y2 = np.cumsum(np.random.random(numdata) - 0.5) * 0.002
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(t, y1, 'r-', label='y1')
ax2.plot(t, y2, 'g-', label='y2')
ax1.legend()
plt.show()
I know swapping y1 with y2 will make the tracker report y1-values,
but this also places the y1 tickmarks on the right-hand side, which is not what I want to happen.
ax1.plot(t, y2, 'g-', label='y2')
ax2.plot(t, y1, 'r-', label='y1')
Ah, found it: ax.yaxis.set_ticks_position("right").
Instead of trying to "control the tracker", you can swap the location of the y-axes.
ax1.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")
ax1.plot(t, y2, 'g-', label='y1')
ax2.plot(t, y1, 'r-', label='y2')
AFAIK, the tracker always follows ax2 when using twinx.
Please note that if you create an ax3= ax1.twiny() axes after ax1 and ax2, the tracker goes to ax3 and you have again it reporting y1 values.
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(6)
numdata = 100
t = np.linspace(0.05, 0.11, numdata)
y1 = np.cumsum(np.random.random(numdata) - 0.5) * 40000
y2 = np.cumsum(np.random.random(numdata) - 0.5) * 0.002
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(t, y1, 'r-', label='y1')
ax2.plot(t, y2, 'g-', label='y2')
ax1.legend()
ax3 = ax1.twiny()
ax3.set_xticks([])
plt.show()
Related
I am trying to fill colours between two-step line plots. I have tried to do the same using fill_between function with step and interpolate parameters. However, I am not getting the output as expected. I am filling the region between two lines after comparing their values. Below is the code. Any help will be appreciated.
fig = plt.figure()
fig.tight_layout()
plt.subplot(2, 2, 1)
p1 = plt.step(df2['datetime'], df2['T1'], color='b', linewidth=3, where = 'post', label ='P1')
p2 = plt.step(df2['datetime'], df2['T3'], color='m', linewidth=3, where = 'post', label ='P2')
p3 = plt.fill_between(df2['datetime'], df2['T1'],df2['T3'], where = df2['T1'] <
df2['T3'],facecolor="blue", color='blue', alpha=0.25, step = 'post',interpolate = True ,label ='A1')
p4 = plt.fill_between(df2['datetime'], df2['T1'],df2['T3'], where = df2['T1'] >
df2['T3'],facecolor="red", color='red', alpha=0.25, step = 'post',interpolate = True, label ='A2')
plt.ylabel("T1", fontsize=12, color='black')
plt.xlabel("Hour", fontsize=12, color='black')
plt.grid(True)
plt.legend(loc='best',fontsize = 10)
plt.xticks(rotation = 90)
plt.subplot(2, 2, 2)
p1 = plt.step(df2['datetime'], df2[‘T2’], color='k', linewidth=3, where = 'post', label ='P1')
p2 = plt.step(df2['datetime'], df2['T3'], color='m', linewidth=3, where = 'post', label ='P2')
p3 = plt.fill_between(df2['datetime'], df2[‘T2’],df2['T3'], where = df2[‘T2’] <
df2['T3'],facecolor="blue", color='blue', alpha=0.25, step = 'post',label ='A1')
p4 = plt.fill_between(df2['datetime'], df2[‘T2’],df2['T3'], where = df2[‘T2’] >
df2['T3'],facecolor="red", color='red', alpha=0.25, step = 'post',label ='A2')
plt.ylabel("T2", fontsize=12, color='black')
plt.xlabel("Hour", fontsize=12, color='black')
plt.grid(True)
plt.legend(loc='best',fontsize = 10)
plt.xticks(rotation = 90)
I am also attaching my output for reference.
Left plot is with step = post and interpolate = True
Right plot is without interpolate.
As you can see filling in not working as expected near cross overs.
Apparently, fill_between between step plots and using a where parameter doesn't fill as expected.
A workaround is to mimic the step function via np.repeat:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20)
y1 = np.random.rand(20)
y2 = np.random.rand(20)
xx = np.repeat(x, 2)[1:]
yy1 = np.repeat(y1, 2)[:-1]
yy2 = np.repeat(y2, 2)[:-1]
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(18, 4))
for ax in (ax1, ax2, ax3):
ax.step(x, y1, color='r', lw=3, where='post')
ax.step(x, y2, color='b', lw=3, where='post')
ax1.fill_between(x, y1, y2, color='b', alpha=0.3, step='post', where=y1 < y2)
ax1.fill_between(x, y1, y2, color='r', alpha=0.3, step='post', where=y1 > y2)
ax1.set_title('fill_between with step and where')
ax2.fill_between(x, y1, y2, color='b', alpha=0.3, step='post', where=y1 < y2, interpolate=True)
ax2.fill_between(x, y1, y2, color='r', alpha=0.3, step='post', where=y1 > y2, interpolate=True)
ax2.set_title('setting interpolate=True')
ax3.fill_between(xx, yy1, yy2, color='b', alpha=0.3, where=yy1 < yy2)
ax3.fill_between(xx, yy1, yy2, color='r', alpha=0.3, where=yy1 > yy2)
ax3.set_title('mimicking step')
plt.tight_layout()
plt.show()
I want to make the lines of the following graph smooth. I tried to search and it seems that we have to represent the x-axis in terms of a float or some type such as date time. Here since the x-axis are just labels, I could not figure out how I should change my code. Any help is appreciated.
import matplotlib.pyplot as plt
x1 = [">1", ">10",">20"]
y1 = [18,8,3]
y2 = [22,15,10]
y3=[32,17,11]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x1, y1, color='blue', label='Heuristic')
ax1.scatter(x1, y2, color='green', label='SAFE')
ax1.scatter(x1, y3, color='red', label='discovRE')
plt.plot(x1, y2, '.g:')
plt.plot(x1, y1, '.b:')
plt.plot(x1, y3, '.r:')
plt.ylabel('False Positives',fontsize=8)
plt.xlabel('Function instruction sizes',fontsize=8)
plt.legend()
plt.show()
Following is the graph that I get right now.
Maybe you can fit a curve to 'smooth' the curve
import matplotlib.pyplot as plt
x1 = [">1", ">10",">20"]
y1 = [18,8,3]
y2 = [22,15,10]
y3=[32,17,11]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x1, y1, color='blue', label='Heuristic')
ax1.scatter(x1, y2, color='green', label='SAFE')
ax1.scatter(x1, y3, color='red', label='discovRE')
buff_x = np.linspace(0,2,100)
def reg_func(y):
params = np.polyfit(range(len(y)),y,2)
return np.polyval(params,buff_x)
plt.plot(buff_x, reg_func(y2), 'g',linestyle='dotted')
plt.plot(buff_x, reg_func(y1), 'b',linestyle='dotted')
plt.plot(buff_x, reg_func(y3), 'r',linestyle='dotted')
plt.ylabel('False Positives',fontsize=8)
plt.xlabel('Function instruction sizes',fontsize=8)
plt.legend()
plt.show()
as you can see, I use a function reg_func to fit your data, and plot the predicted curves
I have used the following code and in the last the figure of thee subplot why I am having an extra plot(red curve)
fig= plt.figure()
plt.rcParams.update({'font.size':24})
fig, (ax1,ax2,ax3)=plt.subplots(3,1,figsize=
(20,24),sharex='row',sharey='col')
p1=plt.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--")
ax1.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction1,"r--", lw=3.0)
ax2.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction2,"r--", lw=3.0)
ax3.plot(x_axis,Clean_Current_TEST,"g",x_axis,Clean_Current_Prediction3,"b--", lw=3.0)#,x_axis,Clean_Current_Prediction3,"b--"
ax1.legend(p1[:2], ["Experimental Output","Predicted Output1"],loc='best');
ax2.legend(p1[:2], ["Experimental Output","Predicted Output2"],loc='best');
ax3.legend(p1[:2], ["Experimental Output","Predicted Output3"],loc='best');
ax1.title.set_text('Short Circuit Current of Clean Module vs
Time Graph')
fig.text(0.5, 0.01, 'Time(hr)', ha='left', va='center')
fig.text(0.01, 0.5, 'Clean Module Current(mA)', ha='center',
va='center', rotation='vertical')
[enter image description here][1]
plt.show()
plt.tight_layout(pad=1)
(The last figure of the subplot)
Here is a minimum, reproducible example that recreates the problem you are experiencing:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,1,20)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.arctan(x)
fig, (ax1,ax2) = plt.subplots(2,1)
plt.plot(x, y3, "g", x, y1, "r--")
ax1.plot(x, y3, "g", x, y1, "r--", lw=3.0)
ax2.plot(x, y3, "g", x, y2, "b--", lw=3.0)
plt.show()
It seems that the plt.plot function actually plots its graph on the last axis. This is why you are seeing the "red" graph on the last axis, even though the line that plots on axis 2 explicitly calls for a "blue" line. In general, if you are plotting separate things on various axes, then you should not use the convenience methods in matplotlib.pyplot, and should instead use the methods on the Axis objects.
fig, (ax1,ax2) = plt.subplots(2,1)
ax1.plot(x, y3, 'g', x, y1, "r--", lw=3.0)
ax2.plot(x, y3, 'g', x, y2, "b--", lw=3.0)
plt.show()
I was trying to make a graph with two secondary vertical axis (y-axis), with python matplotlib.
I was using twinx() method, where one of the two new axis is with the default behavior (labels on the right)
and the other with labels on the left, like the example of tan(x) on the figure bellow (created in an specific software).
Is there an easy way to do that? I'm not restricted to use the twinx() method, if there is another way
Here is a way to add two secondary y-axis, one towards the inside:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(.5, 10, 1000)
y1 = np.cos(x)
y2 = np.sin(2 * x)
y3 = np.clip(np.tan(x * .6), -75, 75)
fig, ax1 = plt.subplots()
color = 'dodgerblue'
ax1.set_ylabel('$cos(x)$', color=color)
ax1.plot(x, y1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'limegreen'
ax2.set_ylabel('$sin(2 x)$', color=color)
ax2.plot(x, y2, color=color)
ax2.tick_params(axis="y", labelcolor=color)
ax3 = ax1.twinx()
color = 'crimson'
ax3.set_ylabel('$tan(.6 x)$', color=color, labelpad=-40)
ax3.plot(x, y3, color=color)
ax3.tick_params(axis="y", labelcolor=color, direction="in", pad=-5)
plt.setp(ax3.get_yticklabels(), ha="right")
ax1.set_xlim(0, 12)
fig.tight_layout()
plt.show()
I want to draw a picture like this one, the top and right axes have different labels and ticks, anyone can help me?
To double both axes you have to use ax1.twinx().twiny().
Here an example:
# Create some mock data
x1 = np.arange(0, 10, 1)
y1 = [random.randint(1,5) for n in x1]
#print(x1,y1)
x2 = np.arange(0, 100, 10)
y2 = [random.randint(10,50) for n in x2]
#print(x2,y2)
fig, ax1 = plt.subplots()
ax1.set_xlabel('x1', color='red')
ax1.set_ylabel('y1', color='red')
ax1.plot(x1, y1, color='red')
ax1.tick_params(axis='both', labelcolor='red')
ax2 = ax1.twinx().twiny() #here is the trick!
ax2.set_xlabel('x2', color='blue')
ax2.set_ylabel('y2', color='blue')
ax2.plot(x2, y2, color='blue')
ax2.tick_params(axis='both', labelcolor='blue') #y2 does not get blue... can't yet figure out why
plt.show()
Here the result:
Since both datasets are completely independent, one would probably not use twin axes here. Instead, just use two different axes.
import numpy as np
import matplotlib.pyplot as plt
# Create some mock data
x1 = np.linspace(0,1,11)
y1 = np.random.rand(11)
x2 = np.linspace(1,0,101)
y2 = np.random.rand(101)*20+20
fig, ax1 = plt.subplots()
ax2 = fig.add_subplot(111, label="second axes")
ax2.set_facecolor("none")
ax1.set_xlabel('x1', color='red')
ax1.set_ylabel('y1', color='red')
ax1.plot(x1, y1, color='red')
ax1.tick_params(colors='red')
ax2.set_xlabel('x2', color='blue')
ax2.set_ylabel('y2', color='blue')
ax2.plot(x2, y2, color='blue')
ax2.xaxis.tick_top()
ax2.xaxis.set_label_position('top')
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position('right')
ax2.tick_params(colors='blue')
for which in ["top", "right"]:
ax2.spines[which].set_color("blue")
ax1.spines[which].set_visible(False)
for which in ["bottom", "left"]:
ax1.spines[which].set_color("red")
ax2.spines[which].set_visible(False)
plt.show()
You should use twinx and twiny functions, take a look at this link