(Oct-13-2019, 10:31 PM)ericvrocha Wrote: For a program that I am creating, I need to use graphs.
The graphs are for Pressure X Depth for engineering.
For better visualization, we usually use a negative Y axis for Depth, so the lines will go to the bottom meaning they are going deeper as the depth increases.
[ ... ]
Does anybody knows how to invert the Y axis? Even if I use negative values, they will still go "up".
Also, what is the best way to change the legend location? When I run as it is it shows almost in the middle of the graphs, I would like to have it in the bottom right like usual, or somewhere "cleaner". I tried using commands like "loc" for locations but did not work for this one.
Hi!
I'm also a newbie, so probably my code could be improved, but I'm going to try to address your graphs questions.
On your original program:
# no_pressure_01.py
#
import matplotlib.pyplot as plt
x1 = [9513, 9948, 10384, 10819]
y1 = [4700, 4860, 5020, 5180]
# plotting the line 1 points
plt.plot(x1, y1, label="Press Fluid Com Perda")
# line 2 points
x2 = [11417, 11472, 11526, 11580]
y2 = [4700, 4860, 5020, 5180]
# plotting the line 2 points
plt.plot(x2, y2, label="Press Fluid Form")
# line 3 points
x3 = [12874, 13219, 13654, 14090]
y3 = [4700, 4860, 5020, 5180]
# plotting the line 3 points
plt.plot(x3, y3, label="Press Fluid Sem Perda")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Pressure (psi)')
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()
If you change what appears on lines 30-31:
# show a legend on the plot
plt.legend()
to:
# show a legend on the plot
plt.legend(bbox_to_anchor=(1, 1),
bbox_transform=plt.gcf().transFigure)
then the output will be:
where you can see that the legend box has changed its location on the chart.
If you change what appears on lines 30-31:
# show a legend on the plot
plt.legend()
to:
# show a legend on the plot
plt.legend(bbox_to_anchor=(0., 1.01, 1., .102), loc='lower left',
ncol=2, mode="expand", borderaxespad=0.)
then the output will be:
where you can see that the legend box has changed again its location on the chart. Now it is on top.
If you change what appears on lines 30-31:
# show a legend on the plot
plt.legend()
to:
# show a legend on the plot
plt.legend(bbox_to_anchor=(1.01, 1), loc='upper left', borderaxespad=0.)
then the output will be:
where you can see that the legend box has changed again its location on the chart. Now it is on the right, but a bit off the visible area.
To sort this problem, you can see in the matplot image, a button, named '
Configure subplots', that looks like 3 lines with sliders on them, just between the magnifying glass button and the save button:
If you click on it with the left button of the mouse, a slider window will pop up, where moving the different bars to the left or to the right, you can see the effect that is produced in the visualization of the image:
Moving the slider on the
bar named 'right' to the left, you can make the legend box completely visible:
If we modify a bit more your program, like this:
# no_pressure_05.py
#
import matplotlib.pyplot as plt
x1 = [9513, 9948, 10384, 10819]
y1 = [4700, 4860, 5020, 5180]
# plotting the line 1 points
line1, = plt.plot(x1, y1, label="Press Fluid Com Perda")
# line 2 points
x2 = [11417, 11472, 11526, 11580]
y2 = [4700, 4860, 5020, 5180]
# plotting the line 2 points
line2, = plt.plot(x2, y2, label="Press Fluid Form")
# line 3 points
x3 = [12874, 13219, 13654, 14090]
y3 = [4700, 4860, 5020, 5180]
# plotting the line 3 points
line3, = plt.plot(x3, y3, label="Press Fluid Sem Perda")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Pressure (psi)')
# show a legend on the plot
# Create a legend for each line.
first_legend = plt.legend(handles=[line1], loc='upper left')
second_legend = plt.legend(handles=[line2], loc='center')
# Add the legend manually to the current Axes.
ax = plt.gca().add_artist(first_legend)
ax = plt.gca().add_artist(second_legend)
# Create another legend for the third line.
plt.legend(handles=[line3], loc='lower right')
# function to show the plot
plt.show()
where line:
plt.plot(x1, y1, label="Press Fluid Com Perda")
has been changed into this one:
line1, = plt.plot(x1, y1, label="Press Fluid Com Perda")
And in a similar way, line:
plt.plot(x2, y2, label="Press Fluid Form")
into:
line2, = plt.plot(x2, y2, label="Press Fluid Form")
and line:
plt.plot(x3, y3, label="Press Fluid Sem Perda")
into:
line3, = plt.plot(x3, y3, label="Press Fluid Sem Perda")
and a little bigger change for line:
plt.legend(bbox_to_anchor=(1.01, 1), loc='upper left', borderaxespad=0.)
into:
# Create a legend for each line.
first_legend = plt.legend(handles=[line1], loc='upper left')
second_legend = plt.legend(handles=[line2], loc='center')
# Add the legend manually to the current Axes.
ax = plt.gca().add_artist(first_legend)
ax = plt.gca().add_artist(second_legend)
# Create another legend for the third line.
plt.legend(handles=[line3], loc='lower right')
These changes to the program put a label to each line:
Going back to the original program, and changing just this line 31:
plt.legend()
to:
plt.legend(bbox_to_anchor=(0, -0.3), loc='center left')
then the output, after clicking on the
'Configure subplots' button, and
after moving the slider on the 'bottom' bar to the right as much as necessary, the legend box will appear from the bottom of the image:
where you can see that the legend box is now at the bottom on the chart.
If you want to display negative values of pressure, maybe you could use something like this:
# no_pressure_07.py
#
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
fig = plt.figure()
fig.subplots_adjust(right=0.85)
ax = SubplotZero(fig, 1, 1, 1)
fig.add_subplot(ax)
# make right and top axis invisible
ax.axis["right"].set_visible(False)
ax.axis["top"].set_visible(False)
ax.axis["bottom"].set_visible(True)
ax.axis["left"].set_visible(True)
# make xzero axis (horizontal axis line through y=0) visible.
#ax.axis["xzero"].set_visible(True)
#ax.axis["xzero"].label.set_text("'x' axis")
ax.set_xlim(9000, 15000)
ax.set_ylim(-1000, 6000)
x1 = [9513, 9948, 10384, 10819]
y1 = [4700, 4860, 5020, 5180]
# plotting the line 1 points
plt.plot(x1, y1, label="Press Fluid Com Perda")
# line 2 points
x2 = [11417, 11472, 11526, 11580]
y2 = [4700, 4860, 5020, 5180]
# plotting the line 2 points
plt.plot(x2, y2, label="Press Fluid Form")
# line 3 points
x3 = [12874, 13219, 13654, 14090]
y3 = [4700, 4860, 5020, 5180]
# plotting the line 3 points
plt.plot(x3, y3, label="Press Fluid Sem Perda")
# naming the x axis
plt.xlabel("'x' axis")
# naming the y axis
plt.ylabel("'y' axis")
# giving a title to my graph
plt.title('Pressure (psi)')
# show a legend on the plot
plt.legend(bbox_to_anchor=(0, -0.3), loc='center left')
# function to show the plot
plt.show()
then the output, after clicking on the
'Configure subplots' button, and
after moving the slider on the 'bottom' bar to the right as much as necessary, the legend box will appear from the bottom of the image:
You could also consider
subplots. In the following example,
I've just made up some 'x' values for also
some made up 'negative values of pressure' and put these on a subplot:
# no_pressure_09_with_negativePressure.py
#
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
fig, (ax1, ax2) = plt.subplots(2)
ax = SubplotZero(fig, 1, 1, 1)
# make right and top axis invisible
ax.axis["right"].set_visible(False)
ax.axis["top"].set_visible(False)
ax.axis["bottom"].set_visible(True)
ax.axis["left"].set_visible(True)
ax1.set_xlim(9000, 15000)
ax1.set_ylim(4650, 5250)
x1 = [9513, 9948, 10384, 10819]
y1 = [4700, 4860, 5020, 5180]
# plotting the line 1 points
ax1.plot(x1, y1, label="Press Fluid Com Perda")
# line 2 points
x2 = [11417, 11472, 11526, 11580]
y2 = [4700, 4860, 5020, 5180]
# plotting the line 2 points
ax1.plot(x2, y2, label="Press Fluid Form")
# line 3 points
x3 = [12874, 13219, 13654, 14090]
y3 = [4700, 4860, 5020, 5180]
# plotting the line 3 points
ax1.plot(x3, y3, label="Press Fluid Sem Perda")
ax2.set_xlim(9000, 15000)
ax2.set_ylim(-850, -450)
# line 4 points (negative pressure)
x4 = [9100, 9200, 9300, 9400]
y4 = [-500, -600, -700, -800]
# plotting the line 4 points (negative pressure)
ax2.plot(x4, y4, 'tab:red', label="Negative Pressure")
# naming the x axis
plt.xlabel("'x' axis")
# naming the y axis
plt.ylabel("'y' axis")
# show a legend on the plot
plt.legend(bbox_to_anchor=(0, -0.3), loc='center left')
# function to show the plot
plt.show()
then the output, after clicking on the
'Configure subplots' button, and
after moving the slider on the 'bottom' bar to the right as much as necessary, the legend box will appear from the bottom of the image:
I hope this long answer helps you get what you want.
I have based my answer on these sources:
https://matplotlib.org/3.1.1/api/_as_gen...egend.html
https://matplotlib.org/3.1.1/tutorials/i...guide.html
https://matplotlib.org/3.1.1/gallery/sub...ht=subplot
https://matplotlib.org/3.1.1/gallery/sub..._demo.html
All the best,