Matplotlib Plots Lose Transparency when Saving as PS/EPS

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:01:27

2K+ Views

Whenever plots are saved in .eps/.ps, then the transparency of the plots get lost.To compare them, we can take the following Steps −Create x_data and y_data using numpy.Plot x_data and y_data (Step 1), using the plot() method, with less aplha value, to make it more transparent.Use the grid() method to prove the transparency of the line.Save the created plot in .eps format.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x_data = np.linspace(1, 10, 100) y_data = np.sin(x_data) plt.plot(x_data, y_data, c='green', marker='o', alpha=.35, ms=10, lw=1) plt.grid() plt.savefig("lost_transparency_img.eps") plt.show()OutputThe PostScript backend ... Read More

Plot a Gradient Color Line in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:58:32

9K+ Views

To plot a gradient color line in matplotlib, we can take the following steps −Create x, y and c data points, using numpy.Create scatter points over the axes (closely so as to get a line), using the scatter() method with c and marker='_'.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 1000) y = np.exp(x) c = np.tan(x) plt.scatter(x, y, c=c, marker='_') plt.show()Output

Superscript in Python Plots

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:56:42

12K+ Views

To put some superscript in Python, we can take the following steps −Create points for a and f using numpy.Plot f = ma curve using the plot() method, with label f=ma.Add title for the plot with superscript, i.e., kgms-2.Add xlabel for the plot with superscript, i.e., ms-2.Add ylabel for the plot with superscript, i.e., kg.To place the legend, use legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = np.linspace(1, 10, 100) m = 20 f = m*a plt.plot(a, f, c="red", lw=5, label="f=ma") plt.title("Force $\mathregular{kgms^{-2}}$") plt.xlabel("Acceleration $\mathregular{ms^{-2}}$") plt.ylabel("Acceleration $\mathregular{kg}$") ... Read More

Rotate Tick Labels for Seaborn Barplot in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:51:59

8K+ Views

To rotate tick labels for Seaborn barplot, we can take the following steps −Make a dataframe using Pandas.Plot the bar using Seaborn's barplot() method.Rotate the xticks label by 45 angle.To display the figure, use the show() method.Exampleimport pandas import matplotlib.pylab as plt import seaborn as sns import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame({"X-Axis": [np.random.randint(10) for i in range(10)], "YAxis": [i for i in range(10)]}) bar_plot = sns.barplot(x='X-Axis', y='Y-Axis', data=df) plt.xticks(rotation=45) plt.show()Output

Update Matplotlib's imshow Window Interactively

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:50:08

5K+ Views

To plot interactive matplotlib’s imshow window, we can take the following steps −Using the subplots() method, create a figure and a set of subplots.Create an array to plot an image, using numpy.Display the image using the imshow() method.To make a slider axis, create an axes and a slider, with facecolor=yellow.To update the image, while changing the slider, we can write a user-defined method, i.e., update(). Using the draw_idle() method, request a widget redraw once the control returns to the GUI event loop.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import Slider ... Read More

Plot Curves to Differentiate Antialiasing in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:47:44

260 Views

To differentiate antialiasing through curves, we can take the following Steps −Add a subplot to the current figure, using the subplot() method, where nrows=1, ncols=2 and index=1.Plot the curve using the plot() method, where antialiased flag is false and color is red.Place the legend at the upper-left corner using the legend() method.Add a subplot to the current figure, using the subplot() method, where nrows=1, ncols=2 and index=2.Plot the curve using the plot() method, where antialiased flag is true and color is green.Place the legend at the upper-right corner using the legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt ... Read More

Get the Legend as a Separate Picture in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:43:34

4K+ Views

To get the legend as a separate picture, we can take the following steps −Create x and y points using numpy.Using the figure() method, create a new figure, or activate an existing figure for Line plot and Legend plot figures.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using the add_subplot() method at nrow=1, ncols=1 and at index=1.Create line1 and line2 using x, y and y1 points.Place the legend for line1 and line2, set ordered labels, put at center location.Save the figure only with legend using the savefig() method.Exampleimport numpy as np from matplotlib import pyplot as plt x = np.linspace(1, 100, ... Read More

Logarithmic Y-Axis Bins in Python

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:38:49

4K+ Views

To plot logarithmic Y-axis bins in Python, we can take the following steps −Create x and y points using numpy.Set the Y-axis scale using the yscale() method.Plot the x and y points, using the plot() method with linestyle="dashdot" and label="y=log(x)".To activate the label of the line, use the legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 100, 1000) y = np.log(x) plt.yscale('log') plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)") plt.legend() plt.show()OutputRead More

Plot Matplotlib Contour

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:33:48

349 Views

To plot matplotlib contour, we can take the following steps −Create data points for x, y and h using numpy.Using the countourf() method, create a colored 3D (alike) plot.Using the set_over() method, set the color for high out-of-range values when "norm.clip = False".Using the set_under() method, set the color for low out-of-range values when "norm.clip = False".Using the changed() method, call this whenever the mappable is changed to notify all the callbackSM listeners to the "changed" signal.Use the show() method to display the figure.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = ... Read More

Display 3D Plot of a 3D Array Isosurface in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:31:20

1K+ Views

Let's take an example to see how to display a 3D plot of a 3D array isosurface in matplotlib −Exampleimport numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(-5, 5, 0.25) y = np.arange(-5, 5, 0.25) x, y = np.meshgrid(x, y) h = x ** 2 + y ** 2 fig = plt.figure() ax = Axes3D(fig) ax.plot_surface(x, y, h, rstride=1, cstride=1, cmap=plt.cm.rainbow, linewidth=0, antialiased=False) plt.show()Output

Advertisements