Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Little Help With Graphs
#1
Hello everyone!
As a new programmer, I am still learning some stuff.
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.

The code I was thinking about using for the graphs are as below, however I will use my own variables of course.
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.


Thanks in advance.


 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 Perd")

    # 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()
I need the graphs to be like below:
C:\Users\Kituzeira\Desktop\graphs
Reply
#2
Bad link
Reply
#3
(Oct-14-2019, 12:35 AM)Larz60+ Wrote: Bad link

Ugh, not sure why the image did not go lol.
Hopefully this one works
[Image: url?sa=i&source=images&cd=&ved=2ahUKEwiB...1777754191]

https://www.google.com/url?sa=i&source=i...1777754191

Basically the Y axis needs to be below X axis. And the numbers on Y axis would increase from top to bottom, giving the idea of Depth to the graph.

Edit: I could not attach an image of my own, not sure why or how, sorry. So the link above has the same idea, not the same info, but the same idea. X axis above Y axis, and Y axis increases going down even though they are not negative values.
Reply
#4
import matplotlib.pyplot as plt

# plot x axis ticks and labels on top
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True

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 Perd")

# invert the y axis
plt.gca().invert_yaxis()


    # 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()

plt.savefig('plot.png')
    # function to show the plot
plt.show()
   

Is that what you want?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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:

[Image: no-pressure-02.png]

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:

[Image: no-pressure-03.png]

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:

[Image: no-pressure-04.png]

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:

[Image: configure-subplots.png]

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:

[Image: slider-for-no-pressure-04-01.png]

Moving the slider on the bar named 'right' to the left, you can make the legend box completely visible:

[Image: no-pressure-04-after-applying-slider.png]

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:

[Image: no-pressure-05.png]

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:

[Image: no-pressure-06-after-applying-slider.png]

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:

[Image: no-pressure-07-after-applying-slider.png]

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:

[Image: no-pressure-09-after-applying-slider.png]

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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
just to mention that instead of the two lines on the top

# plot x axis ticks and labels on top
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
you can do

# invert the y axis
plt.gca().invert_yaxis()
plt.gca().xaxis.set_label_position('top')
plt.gca().xaxis.set_ticks_position('top')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plot several graphs on the same row Menthix 1 1,026 Mar-18-2022, 05:47 PM
Last Post: deanhystad
  grouped bar graphs tobiasfw 1 1,395 Feb-22-2022, 02:25 PM
Last Post: deanhystad
  How to merge strings through graphs Den 6 3,399 Jun-29-2020, 07:07 AM
Last Post: Den
  Tranforming into .exe with Matplotlib (graphs) ericvrocha 0 2,902 Oct-14-2019, 06:54 PM
Last Post: ericvrocha
  Drawing graphs with matplotlib JRod 5 4,699 Jul-31-2017, 10:00 AM
Last Post: JRod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020