Matplotlib in Python
Matplotlib in Python
How to Generate Random Colors in Matplotlib Plots
You can use the following basic syntax to generate random colors in Matplotlib plots:
1. Generate Random Color for Line Plot
col = (np.random.random(), np.random.random(), np.random.random())
plt.plot(x,...
Matplotlib in Python
How to Adjust Bin Size in Matplotlib Histograms
You can use one of the following methods to adjust the bin size of histograms in Matplotlib:
Method 1: Specify Number of Bins
plt.hist(data, bins=6)
Method 2:...
Matplotlib in Python
How to Adjust Axis Label Position in Matplotlib
You can use the following basic syntax to adjust axis label positions in Matplotlib:
#adjust y-axis label position
ax.yaxis.set_label_coords(-.1, .5)
#adjust x-axis label position
ax.xaxis.set_label_coords(.5, -.1)
The following...
Matplotlib in Python
How to Save Matplotlib Figure to a File (With Examples)
You can use the following basic syntax to save a Matplotlib figure to a file:
import matplotlib.pyplot as plt
#save figure in various formats
plt.savefig('my_plot.png')
plt.savefig('my_plot.jpg')
plt.savefig('my_plot.pdf')
The following...
Matplotlib in Python
How to Increase Plot Size in Matplotlib
You can use the following syntax to increase the size of a single plot in Matplotlib:
import matplotlib.pyplot as plt
#define figure size in (width, height)...
Matplotlib in Python
How to Draw a Horizontal Line in Matplotlib (With Examples)
You can use the following syntax to draw a horizontal line in Matplotlib:
import matplotlib.pyplot as plt
#draw vertical line at y=10
plt.axhline(y=10)
The following examples show how...
Matplotlib in Python
How to Create Multiple Matplotlib Plots in One Figure
You can use the following syntax to create multiple Matplotlib plots in one figure:
import matplotlib.pyplot as plt
#define grid of plots
fig, axs = plt.subplots(nrows=2, ncols=1)
#add...
Subscribe
- Never miss a story with notifications
- Gain full access to our premium content
- Browse free from up to 5 devices at once
Must read