plotting two arrays in python with one being filled with random numbers - python

I am trying to plot two arrays r_mod and gbp in python after importing matplotlib. Array r_mod contains random numbers. When I plot the two array with the command plt.plot(r_mod,gbp,"o"), I get the first figure below which shows the global behavior of the relevant function stored in array gbp. However, when plotting with plt.plot(r_mod,gbp), I get the second figure below which does not show the global behavior of the function.
Can someone tell me how to fix this problem ? I need to plot with lines not with "o" .

Reason for this is that matplotlib is ploting in order of first array. To solve this you need to sort first array.
Remember that the second table, if it is correlated with the first, must not be sorted, but the elements must be moved to their corresponding places from the new order of first array.

Related

Slicing a multidimensional numpy array -> 3D point clusters at different time instances

I have a numpy-array, who's shape is:
(30,40,100,200)
Those are 3D points (30(x-axis)x40(y-axis)x100(z-axis)) for different times (200 in total):
For visualization only (this is not my dataset, the picture comes from here: http://15462.courses.cs.cmu.edu/fall2016/article/35)
Now, I have issues with understanding how I can slice it:
How do I extract a 3D cluster for one specific time, i.e. 140?
From that extracted 3D cluster, how can I plot a 2D x-z cross-section for a specific y-position, i.e.45?
You should read up on basic numpy slicing: https://numpy.org/doc/stable/reference/arrays.indexing.html
How do I extract a 3D cluster for one specific time, i.e. 140?
Just specify the time index, i.e. data[:, :, :, 140]. Be aware that Python indexing starts from 0.
From that extracted 3D cluster, how can I plot a 2D x-z cross-section for a specific y-position, i.e.45?
You can acquire a 2D cross-section by a similar slicing operation, i.e. cluster[:, 45, :]. It can be plotted in various ways depending on the plotting library. imshow() from matplotlib might be one possibility.
Is your question about the data set (how does data categorize and how to get a 3D cluster at a specific time), or about the coding?
If it is about "How to get a cluster at a specific time" it means that your problem is about your particular dataset, which Stackoverflow is not a correct place for these types of question.
If it is about "coding" then define clearly your question and provide us with your code and the problem with it.
Based on your explanation, I think that for each time step, you have a complete set of xyz data, and so the solution is very strait.

python : shift values along a curve

I am trying to figure out a way to do this sort of thing.
I would be happy to hear any suggestions on this type of interpolation.
I have 2 fixed values A and H, and n extra random values in between.
In my example they are on a path, although I would be happy also if I could figure out how to solve it for a straight line.
Passing a global variable to my script I would like to displace these in between points in such a way that they are changed to gradually increasing/decreasing their distance between themselves and the two extremes accordingly (incrementally as shown on the image).

2D-Histogram With A Third Data Set In Python

I have a specific problem that maybe can help me with. I have, currently, 3 arrays of data and I want to make a 2D histogram of the first two while using the third array as values that get summed up in each particular bin. I also want to include a color bar that shows the scale of different colors you see in the histogram.
As a start I looked into using matplotlib.pyplot.hexbin to do this and it seems to work fine but I don't want to have hexagons as the shape of my bins. Is somebody able to point me to some resources on how to do this?

python pyplot connecting points

I am making a pyplot graph with a set of points using:
plt.plot([range(0,10)], [dictionary[key]],'bo')
This correctly draws the points as I expect, however I also want a line to be drawn between these points. I can't find a way to do this with pyplot, I assume it's trivial.
Can someone explain how I can do this?
Try explicitly specifying the properties you want:
plt.plot(range(10),range(10),marker='o',color='b',linestyle='-')
the compact style is nice for interactive stuff, but I find using the keyword arguments makes the code more readable and makes it possible to loop control how the line properties are cycled through when plotting more than one curve on the same graph.
What is dictionary[key] in your code? If it is a scalar then it will make 10 separate lines of length one. I think you may want to really do
plt.plot(np.arange(10),np.ones(10)*dictionary[key],marker='o',color='b',linestyle='-')
or
plt.plot(range(10),[dictionary[key]]*10,marker='o',color='b',linestyle='-')
depending on if you are using numpy or not.
In your case [range(0,10)] is a list of list. Hence, you are plotting 10 points instead of a line. Try
plt.plot(range(0,10), dictionary[key],'bo-')
Yup, just add a "-":
plt.plot([range(0,10)], [dictionary[key]],'bo-')
That will make blue circular points connected by a line.

Python - Matplotlib - Plotting incomplete data on chart

I am trying to write program to generate burndown chart. The x-axis is of dates. The y-axis shows remaining hours on a particular date. The problem is that data is not present for all the dates in advance as it is a burndown chart. So this results in error -
"ValueError: x and y must have same first dimension"
So my question is what default values I can assign to the remaining points on Y-axis?
I will paste actual code if this information is not sufficient. Thanks.
I'm not sure if this is what you want, but by using masked arrays you can avoid plotting specific points. See my answer here.
Or maybe you'd like something more like this, which skips them on the x-axis as well as not plotting them?
I know this question is 2 1/2 years old, but I had a similar problem with plotting graphs with missing data and thought that masked arrays were more complex than they needed to be.
My solution was to put the numpy.inf value at any points where I was missing data and then use the 'o-' option when calling matplotlib.pyplot.plot. This will make lines be broken where you don't have data, but if you have a single data point somewhere missing data on each side of it, you get a circle.
The only downside is that you end up with circles at every point on the line, so it might not be pretty.

Categories

Resources