How to set margins of output figure please?
plt.rcParams["figure.figsize"] = [9, 4]
plt.savefig('figure.pdf')
I would like to have minimal white place to top and bottom margins. In the scrip, I remove axes, can it be the problem?
plt.xticks([])
plt.yticks([])
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
Check out tight_layout().
import matplotlib.pyplot as plt
plt.plot([1, 5, 3])
ax = plt.gca()
plt.xticks([])
plt.yticks([])
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
plt.rcParams["figure.figsize"] = [9, 4]
plt.tight_layout(pad=0)
plt.savefig('figure.pdf')
Related
How can we have (0,0) as below in plotly (python) in scatterplot using go.Scattergl().
How to have some padding before (0,0)?
You have to specify that the lower bound of the range of both axes is 0.
import plotly.graph_objects as go
import numpy as np
x = np.array([1, 2, 3, 4, 5])
fig = go.Figure(data=go.Scatter(x=x, y=x**2))
fig.update_layout(yaxis_range=[0,4])
fig.update_layout(xaxis_range=[0,4])
fig.show()
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()
I have this scatter plot:
I'd like to move the grid in a way that each point (green square) would be surrounded by the grid's cells. For example:
The code to reproduce the plot:
import matplotlib.pyplot as plt
data = [24, 24, 24, 16, 16, 2, 2, 2]
x = list(range(0, len(data)))
y = list(range(0, 25))
plt.scatter(x, data, marker='s', c='g', s=100)
plt.yticks(y)
plt.xticks(x)
plt.grid(True)
plt.show()
Maybe something like the following meets the requirement. You can use the minor ticks for the grid and the major ticks for the labels.
import numpy as np
import matplotlib.pyplot as plt
data = [24, 24, 24, 16, 16, 2, 2, 2]
x = list(range(0, len(data)))
fig, ax = plt.subplots()
ax.scatter(x, data, marker='s', c='g', s=49)
ax.set_yticks(np.arange(25))
ax.set_yticks(np.arange(25+1)-0.5, minor=True)
ax.set_xticks(np.arange(len(data)))
ax.set_xticks(np.arange(len(data)+1)-0.5, minor=True)
ax.grid(True, which="minor")
ax.set_aspect("equal")
plt.show()
I'd like to plot errorbars with categorical X variable. The error bars (upper and lower) are on Y values only.
For example, the code
import numpy as np
import matplotlib.pyplot as plt
x = ["4", "10", "50"]
y = [3, 2, 1]
yerr = np.matrix([[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]])
fig, ax = plt.subplots(1, 1)
ax.errorbar(x, y, yerr=yerr)
plt.show()
plt.close()
gives the following error:
ValueError: In safezip, len(args[0])=3 but len(args[1])=1
The error you get has nothing to do with categorical axis.
You just cannot use a matrix. Use a numpy array,
yerr = np.array([[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]])
or simply a list, there is no need to use numpy here,
yerr = [[1.5, 1.1, 0.9], [1.3, 1.2, 0.8]]
I would like to draw a triangle using python3 module matplotlib.
import numpy as np
import matplotlib.pyplot as plt
X_train = np.array([[1,1], [2,2.5], [3, 1], [8, 7.5], [7, 9], [9, 9]])
Y_train = ['red', 'red', 'red', 'blue', 'blue', 'blue']
plt.figure()
plt.scatter(X_train[:, 0], X_train[:, 1], s = 170, color = Y_train[:])
plt.show()
It displays 6 dots but they are grouped separately in 2 places. (color helps to see it clearly)
There are 2 sets of 3 dots. I want each set(3 dots) be united in the triangle.
How is it possible to implement this? How to build a triangle based on 3 dots using matplotlib?
Any suggestions are appreciated ;)
A triangle is a polygon. You may use plt.Polygon to draw a polygon.
import numpy as np
import matplotlib.pyplot as plt
X = np.array([[1,1], [2,2.5], [3, 1], [8, 7.5], [7, 9], [9, 9]])
Y = ['red', 'red', 'red', 'blue', 'blue', 'blue']
plt.figure()
plt.scatter(X[:, 0], X[:, 1], s = 170, color = Y[:])
t1 = plt.Polygon(X[:3,:], color=Y[0])
plt.gca().add_patch(t1)
t2 = plt.Polygon(X[3:6,:], color=Y[3])
plt.gca().add_patch(t2)
plt.show()