Revolve around a horizontal line in Mathematica? - graphics

I am very new to Mathematica. I have version 11, if that makes a difference.
I am trying to take the area formed by the following lines and and revolve it to form a 3D solid.
y = e^-x
Here is my code, in two sections
f[x_] := E^-x
g[x_] := 1
Plot[{f[x], g[x]}, {x, 0, 2}, Filling -> {1 -> {2}},
PlotLegends -> {"f[x]", "g[x]", "h[y]"}]
Next:
RevolutionPlot3D[(1 - f[x]) , {x, 0, 2}, RevolutionAxis -> "X"]
Here is the 2D and 3D representations:
The 2D one is correct, but not the 3D. I want to rotate the area about y=2 (horizontal line) as to form a shape with a hole in the center. I don't know how to set the axis of rotation to anything other than an axis line. I just want y=2.
How do you accomplish this?

RevolutionPlot3D isn't the right tool for what you want for 2 reasons. First, you want to rotate a 2D region not a line. Second, you want to rotate around a line that isn't one of the axes. RegionPlot3D is the built-in tool for the job. You can easily set up your region as a boolean region, just think about the conditions that the radius x^2 + y^2 has to satisfy
RegionPlot3D[
1 < z^2 + y^2 < (2 - Exp[-x])^2, {x, 0, 2}, {y, -3, 3}, {z, -3, 3}]
I showed the result from 2 different angles to point out the shortcomings of RegionPlot3D. You could improve this result by using a high value for the PlotPoints option, but it isn't great. That's why you should use Simon Woods's function contourRegionPlot3D, defined in this post:
contourRegionPlot3D[
1 < z^2 + y^2 < (2 - Exp[-x])^2, {x, 0, 2}, {y, -3, 3}, {z, -3, 3}]

Related

How to increase color resolution in python matplotlib 3D plots

(edited to make the code clearer) I am using Poly3DCollection to make a graph where several polygons in a 3D space have a colour that depends on a value contained in a separate array.
cmap = cm.plasma
quantity = [0.1, 0.11, 5, 10]
colours = cmap(quantity)
for i in range(K):
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
z = [0, 1, 0, 1]
verts = [list(zip(x, y, z))]
ax.add_collection3d(Poly3DCollection(verts, color=colours[i]))
the problem I have is that the resulting image has a very limited colour resolution, and most of the polygons have the same colours.
I understood from this post that it may depend from python automatically using only 7 different colour levels, but unfortunately the solution in the post only applies to 2D plots.
Any idea on how to extend that to 3D plots?

Count number of repeated elements in list considering the ones larger than them

I am trying to do some clustering analysis on a dataset. I am using a number of different approaches to estimate the number of clusters, then I put what every approach gives (number of clusters) in a list, like so:
total_pred = [0, 0, 1, 1, 0, 1, 1]
Now I want to estimate the real number of clusters, so I let the methods above vote, for example, above, more models found 1 cluster than 0, so I take 1 as the real number of clusters.
I do this by:
counts = np.bincount(np.array(total_pred))
real_nr_of_clusters = np.argmax(counts))
There is a problem with this method, however. If the above list contains something like:
[2, 0, 1, 0, 1, 0, 1, 0, 1]
I will get 0 clusters as the average, since 0 is repeated more often. However, if one model found 2 clusters, it's safe to assume it considers at least 1 cluster is there, hence the real number would be 1.
How can I do this by modifying the above snippet?
To make the problem clear, here are a few more examples:
[1, 1, 1, 0, 0, 0, 3]
should return 1,
[0, 0, 0, 1, 1, 3, 4]
should also return 1 (since most of them agree there is AT LEAST 1 cluster).
There is a problem with your logic
Here is an implementation of the described algorithm.
l = [2, 0, 1, 0, 1, 0, 1, 0, 1]
l = sorted(l, reverse=True)
votes = {x: i for i, x in enumerate(l, start=1)}
Output
{2: 1, 1: 5, 0: 9}
Notice that since you define a vote as agreeing with anything smaller than itself, then min(l) will always win, because everyone will agree that there are at least min(l) clusters. In this case min(l) == 0.
How to fix it
Mean and median
Beforehand, notice that taking the mean or the median are valid and light-weight options that both satisfy the desired output on your examples.
Bias
Although, taking the mean might not be what you want if, for say, you encounter votes with high variance such as [0, 0, 7, 8, 10] where it is unlikely that the answer is 5.
A more general way to fix that is to include a voter's bias toward votes close to theirs. Surely that a 2-voter will agree more to a 1 than a 0.
You do that by implementing a metric (note: this is not a metric in the mathematical sense) that determines how much an instance that voted for x is willing to agree to a vote for y on a scale of 0 to 1.
Note that this approach will allow voters to agree on a number that is not on the list.
We need to update our code to account for applying that pseudometric.
def d(x, y):
return x <= y
l = [2, 0, 1, 0, 1, 0, 1, 0, 1]
votes = {y: sum(d(x, y) for x in l) for y in range(min(l), max(l) + 1)}
Output
{0: 9, 1: 5, 2: 1}
The above metric is a sanity check. It is the one your provided in your question and it indeed ends up determining that 0 wins.
Metric choices
You will have to toy a bit with your metrics, but here are a few which may make sense.
Inverse of the linear distance
def d(x, y):
return 1 / (1 + abs(x - y))
l = [2, 0, 1, 0, 1, 0, 1, 0, 1]
votes = {y: sum(d(x, y) for x in l) for y in range(min(l), max(l) + 1)}
# {0: 6.33, 1: 6.5, 2: 4.33}
Inverse of the nth power of the distance
This one is a generalization of the previous. As n grows, voters tend to agree less and less with distant vote casts.
def d(x, y, n=1):
return 1 / (1 + abs(x - y)) ** n
l = [2, 0, 1, 0, 1, 0, 1, 0, 1]
votes = {y: sum(d(x, y, n=2) for x in l) for y in range(min(l), max(l) + 1)}
# {0: 5.11, 1: 5.25, 2: 2.44}
Upper-bound distance
Similar to the previous metric, this one is close to what you described at first in the sense that a voter will never agree to a vote higher than theirs.
def d(x, y, n=1):
return 1 / (1 + abs(x - y)) ** n if x >= y else 0
l = [2, 0, 1, 0, 1, 0, 1, 0, 1]
votes = {y: sum(d(x, y, n=2) for x in l) for y in range(min(l), max(l) + 1)}
# {0: 5.11, 1: 4.25, 2: 1.0}
Normal distribution
An other option that would be sensical is a normal distribution or a skewed normal distribution.
While the other answer provides a comprehensive review of possible metrics and methods, it seems what you are seeking is to find the closest number of clusters to mean!
So something as simple as:
cluster_num=int(np.round(np.mean(total_pred)))
Which returns 1 for all your cases as you expect.

ColorFunction with InterpolationFunction in RegionPlot3D

I am writing to ask a question regarding the implementation of a field-dependent color in a 3d region plot in Mathematica.
Specifically, I have created the following plot, where f[x,y,z] is an interpolating function of a three-dimensional array (this is done to have lower resolution plots with ease, since the amount of data in the array is significant).
The problem I am encountering is that if I run the following instruction:
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> Function[{x, y, z}, Hue[Rescale[f[x, y, z], {0, 1}]]]]
The color is not imposed correctly (i get a region of uniform color). If I utilize a function g instead (can be any function, e.g. the norm of the point position) inside the Hue, so that
Hue[Rescale[g[x, y, z], {0, 1}]]
the color information is passed correctly. I assume that I am making a mistake with the handling of InterpolatingFunction objects. How should this problem be handled?
Any help is appreciated.
RegionPlot3D passes 4 arguments to ColorFunction :
Try this (just adding a dummy arg to Function )
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> Function[{x, y, z, p}, Hue[Rescale[f[x, y, z], {0, 1}]]]]
or like this:
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> ( Hue[Rescale[f[#1, #2, #3 ], {0, 1}]] &)]

Uniformly distribute Points within an object using Graphics in Mathematica

Considering :
preferred ={{1, 1, 63}, {2, 1, 44}, {3, 1, 27}, {4, 1, 33}, {5, 1, 33}}
frmWidth = 20.9067;
frmHeight = 15.68;
I am displaying 5 types of stimuli 2 by 2. Subjects must choose the one they prefer. Each type of stimuli is displayed 80 times so :
{1,1,63} indicates that the stimuli Cond 1 was preferred 63 times out of the 80 times it was displayed.
{3, 1, 27} indicates that the stimuli Cond 3 was preferred 27 times out of the 80 times it was displayed.
Cond1 refers to center of the screen
Cond2 refers to Top-Left Quadrant
Cond3 refers to Top-Right Quadrant
Cond4 refers to Bottom-Left Quadrant
Cond5 refers to Bottom-Right Quadrant
I would like to express this showing results.
This is what I have done :
Graphics[{
Black, EdgeForm[{Thin, LightGray}],
Rectangle[{-1, -1}, {frmWidth + 1, frmHeight + 1}],
PointSize[0.03],
Yellow,
Point#Tuples[{Range[0, frmWidth/2, frmWidth/19],
Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[5, 3]]]],
Red,
Point#Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19],
Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[4, 3]]]],
Green,
Point#Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19],
Range[frmHeight/2, frmHeight, frmHeight/14]}][[;; preferred[[3, 3]]]],
Orange,
Point#Tuples[{Range[0, frmWidth/2, frmWidth/19],
Range[frmHeight/2, frmHeight, frmHeight/14]}][[;;
preferred[[2, 3]]]],
Blue,
Point#Tuples[{Range[frmWidth/4, 3/4 frmWidth, frmWidth/19],
Range[frmHeight/4, 3/4 frmHeight, frmHeight/14]}][[;;
preferred[[1, 3]]]]
}]
Problem is the rectangles are gradually filled with points from left to right, instead of the points being uniformly located.
Consider the following :
Graphics[{
White, EdgeForm[Thick],
Rectangle[{0, 0}, {frmWidth, frmHeight}],
Orange, Opacity[.5],
Rectangle[{0, frmHeight/2}, {frmWidth/2, frmHeight}, RoundingRadius -> 3],
Green,
Rectangle[{frmWidth/2, frmHeight/2}, {frmWidth, frmHeight},RoundingRadius -> 3],
Red,
Rectangle[{frmWidth/2, 0}, {frmWidth, frmHeight/2}, RoundingRadius -> 3],
Yellow,
Rectangle[{0, 0}, {frmWidth/2, frmHeight/2}, RoundingRadius -> 3],
Blue,
Rectangle[{frmWidth/4, frmHeight/4}, {3/4 frmWidth, 3/4 frmHeight}, RoundingRadius -> 3]
}]
Now I would like to fill those edge rounded rectangles with the points but have the density changing rather than the part of the rectangles that are filled.
Below is something very ugly I draw in PPT :
Ideally, the shapes filled with Points could be of any kind.
Points would not overlap.
Please let me know alternative ideas.
OK, try this:
Manipulate[ld = Floor[Sqrt[n]];
Graphics[
{{EdgeForm[Dashed], White,
Polygon[{{0, 0}, {0, h}, {w, h}, {w, 0}}]},
Point[Flatten[#, 1] &#
Table[{x, y}, {x, 0, w, w/ld}, {y, 0, h, h/ld}]] },
PlotRange \[Rule] {{-1, 20}, {-1, 20}}],
{{n, 25}, 10, 100, 1},
{{h, 10}, 5, 20},
{{w, 10}, 5, 20}]
typical configuration:
(the code I gave lets you control the total number and size of the box via sliders)
Given that your rectangles are rather small, the easiest solution is to use
RandomSample[ allPointsInAnObject ]
Kind of like so:
Graphics[{Circle[{0, 0}, 11], PointSize[0.02],
Point[RandomSample[
Cases[Outer[List, Range[-11, 11], Range[-11, 11]], {x_, y_} /;
x^2 + y^2 <= 11^2, {2}], 50]]}]

Vertical alignment of plots in mathematica via GraphicsColumn

I have an annoying problem using GraphicsColumn() in Mathematica to combine several DateList plots in a single column. I need them to be correctly aligned as they display different timeseries for the same period, but as it turns out the size of the frame of each plot gets automatically resized depending on the length of the Y-axis labels. So combining a plot with 5-figure labels and one with 2-figure labels will totally jeopardise the vertical alignment. I tried several tweaks (e.g. setting width or max width via ImageSize), unfortunately they all seem to apply to the size of the graphic as a whole, not the actual frame. I don't seem to find a way to control the size of the frame itself. Any ideas?
I suspect you want to set the ImagePadding option:
GraphicsColumn[{
Plot[Sin[x], {x, 0, 2 Pi}, ImagePadding -> 20, AxesLabel -> {"x", "very very loooooooong"}],
Plot[Sin[x], {x, 0, 2 Pi}, ImagePadding -> 20, AxesLabel -> {"x", "shrt"}]
}]
I am not sure how you are labeling the graph, but this method should work if you set the value high enough to show the whole label.
You could try the the LevelScheme Multipanel command.
Here's the example given in their documentation and LevelSchemeExamples.nb:
Figure[
{ScaledLabel[{0.5, 1}, "Lissajous curves", FontSize -> 18, Offset -> {0, 1}],
Multipanel[{{0, 1}, {0, 1}}, {2, 2},
XPlotRanges -> {{-1.5, 1.5}, {-Pi/2, 8*Pi + Pi/2}},
YPlotRanges -> {-1.5, 1.5},
XFrameLabels -> {textit["x"], textit["t"]}, BufferB -> 2.5,
YFrameLabels -> textit["y"], BufferL -> 3,
TickFontSize -> 10,
XFrameTicks -> {LinTicks[-2, 2, 1, 5], LinTicks[-Pi, 9*Pi, Pi, 4,
TickLabelFunction -> (Rationalize[#/Pi]*Pi &)]},
YFrameTicks -> LinTicks[-2, 2, 1, 5],
XPanelSizes -> {1, 2.5}, XGapSizes -> {0.1},
YPanelSizes -> {1, 1}, YGapSizes -> {0.1},
Background -> Wheat, PanelLetterBackground -> Wheat
],
FigurePanel[{1, 1}],
RawGraphics[ParametricPlot[{Cos[1*t], Cos[1*t - Pi/2]}, {t, 0, 2*Pi}]],
FigurePanel[{1, 2}],
RawGraphics[Plot[Cos[1*t], {t, 0, 8*Pi}], Dashing -> Automatic],
RawGraphics[Plot[Cos[1*t - Pi/2], {t, 0, 8*Pi}]],
FigurePanel[{2, 1}, PanelLetterBackground -> None],
RawGraphics[ParametricPlot[{Cos[1*t], Cos[4*t - Pi/2]}, {t, 0, 2*Pi}]],
FigurePanel[{2, 2}],
RawGraphics[Plot[Cos[1*t], {t, 0, 8*Pi}], Dashing -> Automatic],
RawGraphics[Plot[Cos[4*t - Pi/2], {t, 0, 8*Pi}]],
},
PlotRange -> {{-0.1, 1.1}, {-0.1, 1.1}},
ImageSize -> 72*2*{3.6, 2.1}
]

Resources