Found 10476 Articles for Python

How to show mouse release event coordinates with Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:43:06

534 Views

To show mouse release event coordinates with matplotlib, we can take the following steps−Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Plot a line in the range of 10.Bind the function *onclick* to the event *button_release_event*.Print event and its x and y data.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams['backend'] = 'TkAgg' plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def onclick(event): print(event.button, event.xdata, event.ydata) fig, ax = plt.subplots() ax.plot(range(10)) fig.canvas.mpl_connect('button_release_event', onclick) plt.show()OutputMouseButton.LEFT 4.961566107601828 1.6644009000562534 MouseButton.LEFT 6.782345894140708 3.7026907931745727 MouseButton.LEFT 2.98552602918754 7.177807987999249Read More

How do I change the axis tick font in a Matplotlib plot when rendering using LaTeX?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:45:24

540 Views

To change the axis tick font in matplotlib when rendering using LaTeX, we can take the following Steps −Create x and y data points using numpy.Using subplot() method, add a subplot to the current figure.Set x and y ticks with data points x and y using set_xticks and set_yticks methods, respectively.Plot x and y using plot() method with color=red.To set bold font weight, we can use LaTeX representation.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 2, 3, 4]) y = np.exp(x) ax1 = ... Read More

How to plot a bar graph in Matplotlib from a Pandas series?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:45:45

2K+ Views

To plot a bar graph from a Pandas series in matplotlib, we can take the following Steps −Make a dictionary of different keys, between the range 1 to 10.Make a dataframe using Pandas data frame.Create a bar plot using plot() method with kind="bar".To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True d = {'y=1/x': [1 / i for i in range(1, 10)],    'y=x': [i for i in range(1, 10)],    'y=x^2': [i * i for i in range(1, 10)],    'y=x^3': [i * i * ... Read More

Plot a circle with an edgecolor in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:46:06

2K+ Views

To plot a circle with an edgecolor in matplotlib, we can take the following Steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a circle instance using Circle() class with an edgecolor and linewidth of the edge.Add a circle path on the plot.To place the text in the circle, we can use text() method.Scale the X and Y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ... Read More

How to rotate tick labels in a subplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:44:05

17K+ Views

To rotate tick labels in a subplot, we can use set_xticklabels() or set_yticklabels() with rotation argument in the method.Create a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set ticks on the X and Y axes using set_xticks and set_yticks methods, respectively, and the list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) and rotation=45 using set_xticklabels() and set_yticklabels().To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add ... Read More

How to make longer subplot tick marks in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:42:47

3K+ Views

To make longer subplot tick marks in matplotlib, we can use tick_params() method for minor and major ticks length and width.StepsAdd a subplot to the current figure using subplot() method.Plot a range(2) values for x and y data points.Turn the minor ticks of the colorbar ON without extruding into the "extend regions".Use tick_params for changing the appearance of ticks and tick labels.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ax1 = plt.subplot() ax1.plot(range(2), range(2), linewidth=2) ax1.minorticks_on() ax1.tick_params('both', length=20, width=2, which='major') ax1.tick_params('both', length=10, width=1, which='minor') plt.show()OutputRead More

How to obtain the same font in Matplotlib output as in LaTex output?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:35:52

299 Views

To make bold font weight LaTeX axes label in matplotlib, we can take the following steps−Create data points for x.Create data points for y, i.e., y=sin(x).Plot the curve x and y with LaTex representation.To activate the label, use legend() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, font_manager as fm fprop = fm.FontProperties(fname='/usr/share/fonts/truetype/malayalam/Karumbi.ttf') plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 1000) y = np.sin(x) plt.plot(x, y, label=r'$\sin (x)$', c="red", lw=2) plt.title(label=r'$\sin (x)$', fontproperties=fprop) plt.show()OutputRead More

How I can get a Cartesian coordinate system in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:34:21

822 Views

To plot a Cartesian coordinate system in matplotlib, we can take the following Steps −Initialize a variable (N) with a value.Create random data points for x and y.Plot the points using scatter method with x and y data points.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True N = 50 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y) plt.show()Output

Bold font weight for LaTeX axes label in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:37:05

3K+ Views

To make bold font weight LaTeX axes label in matplotlib, we can take the following steps−Create x and y data points using numpy.Using subplot() method, add a subplot to the current figure.Set x and y ticks with data points x and y using set_xticks and set_yticks methods, respectively.Plot x and y using plot() method with color=red.To set bold font weight, we can use LaTeX representation.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, font_manager as fm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams["font.fantasy"] = "Comic Sans MS" x = np.array([1, 2, 3, ... Read More

How do I plot multiple X or Y axes in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:34:01

10K+ Views

To plot multiple X or Y axis, we can use twinx() or twiny() methods, we can take the following Steps −Using subplots() method, create a figure and a set of subplots.Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales.Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2.Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.Using tight_layout() method, adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ... Read More

Advertisements