Issue
I am trying to use matplotlib to draw a curve to represent its changing trend. Here are my code:
import matplotlib.pyplot as plt
x = [3.0589998800678586e-07, 3.575999869553925e-07, 4.17199998992146e-07, 0.9999997019767761, 5.364000230656529e-07]
y = [1509, 108, 99, 88, 85]
plt.plot(x, y)
plt.show()
plt.save("./frequency.png")
Here is the graph I get: picture that I got
There are not any same values. I want to know where is the problem?
Solution
To consolidate the comments, there are two problems here:
- The
x
values are not sorted - The
x
values are very small, so on a scale of0
to1
, the very smallx
values all appear seeemingly at the same spot.
You can correct #1 by sorting the values:
x, y = zip(*sorted(zip(x, y), key=lambda pair: pair[0]))
By zipping x
and y
together here for the sort, you are ensuring that all of the coordinate pairs remain the same, they are just ordered separately. You can correct #2 using
plt.xscale("log")
Altogether:
x, y = zip(*sorted(zip(x, y), key=lambda pair: pair[0]))
plt.plot(x, y)
plt.xscale("log")
plt.show()
plt.savefig("frequency.png")
to get
Note: I actually had to remove plt.show()
to get the saved figure to not appear blank.
Answered By - Kraigolas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.