I just want to understand the basic parameters and what they do specifically - width, height, angle, theta1, theta 2. I followed the official documentation and I understood what the centre is, but I don't get what the theta 1 or 2 does, or the angle does or what the length of the horizontal or vertical axis means.
I tried experimenting with the parameters using different numbers but failed to hit upon an accurate result.
I'm trying to create the arc of the 3-point area in the basketball court
The Arc type is a subclass of Ellipse, extended to add two values theta1 and theta2. The behaviour of angle is the same for both Ellipse and Arc and determines the angle at which the ellipse is drawn.
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
fig = plt.figure(figsize=(2,5))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 50)
ax.set_xlim(0, 20)
ax.axis('off')
a = Ellipse((10, 45), 10, 3, 0, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 40), 10, 3, 10, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 35), 10, 3, 20, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 30), 10, 3, 30, color='red', lw=1)
ax.add_patch(a)
for a in range(0, 360, 40):
a = Ellipse((10, 20), 10, 3, a, color='red', lw=1, fc='none')
ax.add_patch(a)
This produces —
Note that for a perfect circle (an ellipse of equal height and width) this makes no difference (as a circle is rotationally symmetrical).
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
fig = plt.figure(figsize=(2,4))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 40)
ax.set_xlim(0, 20)
ax.axis('off')
a = Ellipse((10, 25), 10, 10, 0, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 10), 10, 10, 45, color='red', lw=1)
ax.add_patch(a)
Both circles are the same.
The Arc documentation for the matplotlib.patches.Arc explains that theta 1 & 2 are —
theta1, theta2 : float, optional
Starting and ending angles of the arc in degrees. These values are relative to angle, .e.g. if angle = 45 and theta1 = 90 the absolute starting angle is 135. Default theta1 = 0, theta2 = 360, i.e. a complete ellipse.
The key statement there is "Default theta1 = 0, theta2 = 360, i.e. a complete ellipse." — these parameters are used to draw partial ellipses, to create an arc. theta1 is the angle (or position on) the ellipse at which to start drawing, and theta2 is when to stop. Note that the calculation of the ellipse is unaffected.
The following code draws a series of arcs which should make the logic apparent —
from matplotlib import pyplot as plt
from matplotlib.patches import Arc
fig = plt.figure(figsize=(2,5))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 50)
ax.set_xlim(0, 20)
ax.axis('off')
# A complete ellipse, using theta1=0, theta2=360.
a = Arc((10, 45), 10, 3, 0, 0, 360, color='red', lw=1)
ax.add_patch(a)
# Reduce theta2 to 350, last 10 deg of ellipse not drawn.
a = Arc((10, 40), 10, 3, 0, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Rotate the ellipse (angle=90), theta1 & theta2 are relative to start angle & rotate too.
a = Arc((10, 30), 10, 3, 90, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Rotate the ellipse (angle=180), as above.
a = Arc((10, 20), 10, 3, 180, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Draw the top half of the ellipse (theta 0-180 deg).
a = Arc((10, 10), 10, 3, 0, 0, 180, color='red', lw=1)
ax.add_patch(a)
# Draw the bottom half of the ellipse (theta 180-360 deg).
a = Arc((10, 5), 10, 3, 0, 180, 360, color='red', lw=1)
ax.add_patch(a)
This produces the following image, with arcs drawn above going from top to bottom. Compare with the comments in the code for explanation.
Related
My legend now shows,
I want to add my label in legend, from 0 to 7, but I don't want to add a for-loop in my code and correct each label step by step, my code like that,
fig, ax = plt.subplots()
ax.set_title('Clusters by OPTICS in 2D space after PCA')
ax.set_xlabel('First Component')
ax.set_ylabel('Second Component')
points = ax.scatter(
pca_2_spec[:,0],
pca_2_spec[:,1],
s = 7,
marker='o',
c = pred_pca_2_spec,
cmap= 'rainbow')
ax.legend(*points.legend_elements(), title = 'cluster')
plt.show()
Assuming pred_pca_2_spec is some np.array with values [0, 5, 10, 15, 20, 30, 35] to change the values of these to be in the range 0-7, simply divide (each element) by 5.
Sample Data:
import numpy as np
from matplotlib import pyplot as plt
np.random.seed(54)
pca_2_spec = np.random.randint(-100, 300, (100, 2))
pred_pca_2_spec = np.random.choice([0, 5, 10, 15, 20, 25, 30, 35], 100)
Plotting Code:
fig, ax = plt.subplots()
ax.set_title('Clusters by OPTICS in 2D space after PCA')
ax.set_xlabel('First Component')
ax.set_ylabel('Second Component')
points = ax.scatter(
pca_2_spec[:, 0],
pca_2_spec[:, 1],
s=7,
marker='o',
c=pred_pca_2_spec / 5, # Divide By 5
cmap='rainbow')
ax.legend(*points.legend_elements(), title='cluster')
plt.show()
Using Matplotlib, I would like to plot two lines in one graph, where both lines have their own axis.
Currently, I have:
x = [3, 5, 7, 9, 10, 11, 13, 15, 17, 19, 20, 21, 23, 25, 27, 30, 35, 40, 45, 50]
y1 = [0.658431,0.702574,0.727149,0.760198,0.746229,0.768321,0.763344,0.764400,0.759935,0.758930,0.769689,0.773518,0.764118,0.780918,0.767377,0.766301,0.779629,0.774025,0.773127,0.782209]
y2 = [0.008676, 0.014630, 0.021286, 0.025562, 0.018247, 0.026771, 0.036187, 0.025633, 0.031402, 0.031140, 0.031333, 0.027820, 0.020359, 0.033351, 0.032603, 0.027474, 0.025250, 0.023103, 0.030988, 0.026503]
plt.plot(x, y1, label = "line 1")
plt.plot(x, y2, label = "line 2")
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Y1 and Y2')
plt.legend()
Which results in:
However, both lines look relatively flat as they are represented on the same axis and have a different scale. Instead, I would like an y-axis on the left from 0.66 to 0.78 (to represent y1) and I would like an y-axis on the right between 0 and 0.05 (to represent y2). Then, y1's values can be read on the left axis and y2's values can be read on the right axis and the relative changes are illustrated clearer.
How can I do this?
Based on #rperezsoto's comment, I have the following working code:
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('x-axis')
ax1.set_ylabel('AUC', color='blue')
ax1.plot(x, y1, color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('Standard deviation', color='grey') # we already handled the x-label with ax1
ax2.plot(x, y2, color='grey')
ax2.tick_params(axis='y', labelcolor='grey')
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title('Y1 and Y2')
plt.show()
This is a follow up to my question posted here. A network diagram is added as an inset in matplotlib figure.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.gnm_random_graph(n=10, m=15, seed=1)
nxpos = nx.spring_layout(G, dim=3, seed=1)
nxpts = [nxpos[pt] for pt in sorted(nxpos)]
nx_lines = [(nxpts[i], nxpts[j]) for i, j in G.edges()]
# node values
values = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[30, 80, 10, 79, 70, 60, 75, 78, 65, 10],
[1, .30, .10, .79, .70, .60, .75, .78, .65, .90]]
time = [0.0, 0.1, 0.2] # in seconds
fig, ax = plt.subplots()
ax.plot(
[1, 2, 3], [1, 2, 3],
'go-',
label='line 1',
linewidth=2
)
from mpl_toolkits.mplot3d import (Axes3D)
from matplotlib.transforms import Bbox
rect = [.6, 0, .5, .5]
bbox = Bbox.from_bounds(*rect)
inax = fig.add_axes(bbox, projection = '3d')
# inax.axis('off')
# set angle
angle = 25
inax.view_init(10, angle)
# hide axes, make transparent
# inax.set_facecolor('none')
inax.grid('off')
import numpy as np
# plot 3d
seen = set()
for i, j in G.edges():
x = np.stack((nxpos[i], nxpos[j]))
inax.plot(*x.T, color = 'k')
if i not in seen:
inax.scatter(*x[0], color = 'skyblue')
seen.add(i)
if j not in seen:
inax.scatter(*x[1], color = "skyblue")
seen.add(j)
fig.show()
I would like to change the grid properties i.e set the grid color to red and change line width. I tried inax.grid('on', color='r') but this doesn't change the color. Suggestions on how to change the settings will be really helpful.
You can do it like this:
inax.w_xaxis._axinfo.update({'grid' : {'color': 'red', 'linewidth': 0.8, 'linestyle': '-'}})
inax.w_yaxis._axinfo.update({'grid' : {'color': 'red', 'linewidth': 0.8, 'linestyle': '-'}})
inax.w_zaxis._axinfo.update({'grid' : {'color': 'red', 'linewidth': 0.8, 'linestyle': '-'}})
Output:
I have a code for plotting a cone with some text and arrows like axes. How to change this code to get the bases of the cone looking like circles? Is the problem in the settings of margins? Or is it necessary to define circles in another way?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
FancyArrowPatch.draw(self, renderer)
nn = 400 # number of points along circle's perimeter
theta = np.linspace(0, 2*np.pi, nn)
rho = np.ones(nn)
# (x,y) represents points on circle's perimeter
x = np.ravel(rho*np.cos(theta))
y = np.ravel(rho*np.sin(theta))
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [5, 5]
figsize = (5, 5)
ax = plt.axes(projection='3d') # set the axes for 3D plot
ax.azim = -88 # y rotation (default=270)
ax.elev = 13 # x rotation (default=0)
# Low, high values of z for plotting 2 circles at different elevation
loz, hiz = -15, 15
# Plot two circles
ax.plot(x, y, hiz)
ax.plot(x, y, loz)
# Set some indices to get proper (x,y) for line plotting
lo1,hi1 = 15, 15+nn//2
lo2,hi2 = lo1+nn//2-27, hi1-nn//2-27
# Plot 3d lines using coordinates of selected points
ax.plot([x[lo1], x[hi1]], [y[lo1], y[hi1]], [loz, hiz])
ax.plot([x[lo2], x[hi2]], [y[lo2], y[hi2]], [loz, hiz])
eps = 0.005
ax.plot([0, 0], [0, 0], [0, 20]) # extend in z direction
ax.plot([0-eps, 0], [0-eps, -5], [0-eps, 0]) # extend in y direction
ax.plot([0, 1.3], [0, 0], [0, 0]) # extend in x direction
ax.plot([0+eps, 0.6], [0+eps, -4], [0+eps, 16]) # v vector
ax.plot([0.63, 0.63], [-4, -4], [16, -0.005]) # vertical projection
ax.plot([0+eps, 0.6], [0+eps, -4], [0+eps, -0.005]) # to the beginning
ax.scatter(0, 0, 20, marker=(3, 0, 0), s=100, clip_on=False)
ax.scatter(0, -5, 0, marker=(3, 0, 43), s=100, clip_on=False)
ax.scatter(1.3, 0, 0, marker=(3, 0, 30), s=100, clip_on=False)
ax.scatter(0.6, -4, 16, marker=(3, 0, 80), s=100, clip_on=False)
ax.scatter(0.6, -4, -0.005, marker=(3, 0, 0), s=100, clip_on=False)
a2 = Arrow3D([0.14, -0.515], [-5.581, 1.358], [14.73, 4.983], mutation_scale=20, arrowstyle="-|>", color="k", connectionstyle="arc3,rad=0.3")
ax.add_artist(a2)
ax.text3D(0.23, -5.23, 23.33, r'$A$')
ax.text3D(1.41, 1.29, -2.7, r'$B$')
ax.text3D(-0.31, 1.46, -12.6, r'$C$')
ax.text3D(0.4, -5.48, 17, r'$D$')
ax.text3D(0.64, 1.57, -9.95, r'$E$')
ax.text3D(-0.2, -5.5, 15.73, r'$F$')
# Hide axes
ax._axis3don = False
# Save the figure (.pdf)
margins = { # vvv margin in inches
"left" : 1 / figsize[0],
"bottom" : -2.45 / figsize[1],
"right" : 1 - 0.5 / figsize[0],
"top" : 1 + 1.8 / figsize[1]
}
fig.subplots_adjust(**margins)
plt.savefig('output.pdf')
plt.show()
From this code I got the following output:
The desired output is bases looking like a circle.
This is the view from above:
It is not a circle but an ellipse.
Desired shape of the cone:
Using an example from another post, I'm adding a color bar to a scatter plot. The idea is that both dot hue, and colorbar hue, should conform to the maximum and minimum possible, so that the colorbar can reflect the range of values in the hue:
x= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
y= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
z= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 255]
df = pd.DataFrame(list(zip(x, y, z)), columns =['x', 'y', 'z'])
colormap=matplotlib.cm.viridis
#A continuous color bar needs to be added independently
norm = plt.Normalize(df.z.min(), df.z.max())
sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
sm.set_array([])
fig = plt.figure(figsize = (10,8), dpi=300)
ax = fig.add_subplot(1,1,1)
sb.scatterplot(x="x", y="y",
hue="z",
hue_norm=(0,255),
data=df,
palette=colormap,
ax=ax
)
ax.legend(bbox_to_anchor=(0, 1), loc=2, borderaxespad=0., title='hue from sb.scatterplot')
ax.figure.colorbar(sm).set_label('hue from sm')
plt.xlim(0,255)
plt.ylim(0,255)
plt.show()
Note how the hue from the scatterplot, even with hue_norm, ranges up to 300. In turn, the hue from the colorbar ranges from 0 to 255. From experimenting with values in hue_norm, it seems that matplotlib always rounds it off so that you have a "good" (even?) number of intervals.
My questions are:
Is which one is showing an incorrect range: the scatterplot, the scatterplot legend, or the colorbar? And how to correct it?
How could you retrieve min and max hue from the scatterplot (in this case 0 and 300, respectively), in order to set them as maximum and minimum of the colorbar?
Do you really need to use seaborn's scatterplot(). Using a numerical hue is always quite messy.
The following code is much simpler and yields an unambiguous output
fig, ax = plt.subplots()
g = ax.scatter(df['x'],df['y'], c=df['z'], cmap=colormap)
fig.colorbar(g)