How to plot discrete point with matplotlib? #1682
-
I am doing some calculation with pint and then I need to plot them with matplotlib. For a array of data (likes But when I link some independent points (likes there is my code import matplotlib.pyplot as plt
import numpy as np
import pint
ureg = UnitRegistry()
ureg.setup_matplotlib()
#some calculation......
va = 0.5 * ureg.L
vc = 0.05 *ureg.L
pa = 0.1013 * ureg.MPa
pb = 2*ureg.MPa
pc = 7*ureg.MPa
a_ = np.arange(180,361,10)
v =np.cos(np.radians(a_))
p = pa*(va)**1.34/(v**1.34)
fig, ax = plt.subplots()
ax.xaxis.set_units(ureg.L)
ax.yaxis.set_units(ureg.MPa)
ax.plot(v, p) # draw the line -> right
ax.plot([vc,vc],[pb,pc]) #connect two point -> Wrong Did I do some thing wrong? How can I connect the two points? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
When you do ax.plot([vc.m, vc.m] * vc.units, [pb.m, pc.m_as(pb.units)] * pb.units) |
Beta Was this translation helpful? Give feedback.
When you do
ax.plot([vc,vc],[pb,pc])
, Matplotlib is going to try to convert[ 0.05 * ureg.L, 0.05 * ureg.L]
and[2 * ureg.MPa, 7 * ureg.MPa]
to arrays, which is not going to do what you want (at best it will result in an array of objects). You want to create a single array to pass toplot
, as you did on the line above. One way to do that would be: