Found 1034 Articles for Matplotlib

How to plot two violin plot series on the same graph using Seaborn?

Rishikesh Kumar Rishi
Updated on 08-Oct-2021 12:33:15

1K+ Views

To plot two violin plot series on the same graph using Seaborn, we can take the following Steps.StepsSet the figure size and adjust the padding between and around the subplots.Load an example dataset from the online repository (requires Internet).Create a violin plot using violinplot() method.To display the figure, use Show() method.Example# Import Seaborn and Matplotlib import seaborn as sns from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Load an example dataset tips = sns.load_dataset("tips") # Create a violin plot using Seaborn sns.violinplot(x="day", y="total_bill", hue="time", data=tips) ... Read More

How to autosize text in matplotlib Python?

Rishikesh Kumar Rishi
Updated on 08-Oct-2021 12:31:29

343 Views

To autosize text in matplotlib, we can make a tight layout and rotate the ticks.StepsSet the figure size and adjust the padding between and around the subplots.Plot data points of the range of 10.Make a list of labels.Put ticks and labels on the X-axis with 30 rotation.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 plt.plot(range(10)) labels = [7 * repr(i) for i in range(10)] plt.xticks(range(10), labels, rotation=30) plt.show() OutputIt will produce the following output −

How to change the scale of imshow in matplotlib without stretching the image?

Rishikesh Kumar Rishi
Updated on 08-Oct-2021 12:29:16

14K+ Views

To change the scale of imshow in matplotlib without stretching the image, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create random data points with 4×4 dimension.Display the data as an image, i.e., on a 2D regular raster.Use the extent parameter of imshow to map the image buffer pixel coordinates to a data space coordinate system.Next, set the aspect ratio of the image manually by supplying a value such as "aspect=4" or let it auto-scale by using aspect='auto'. This will prevent stretching of the image. By default,  imshow sets the aspect of ... Read More

Plotting profile histograms in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 07:48:42

525 Views

In a profile histogram, each bin contains the mean of its entries. To plot profile histograms in Python, we can use the regplot method from Seaborn.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use seaborn.regplot to plot the data and a linear regress model fit. Use the parameter x_bins to bin the x variable into discrete bins. Use fit_reg=True to plot the regression model relating the x and y variables.To display the figure, use Show() method.Exampleimport numpy as np import seaborn as sns from matplotlib import pyplot as plt ... Read More

How to use ax.get_ylim() in matplotlib?

Rishikesh Kumar Rishi
Updated on 08-Oct-2021 12:14:22

435 Views

To use ax.get_ylim() method in matplotlib, we can take the following steps.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Create random data points using numpy.Plot y data points using plot() method.Use ax.get_ylim() method to print it.To display the figure, use Show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() # Add an axes to the figure ax = fig.add_subplot(1, 1, 1) ... Read More

Plot a Line Graph for Pandas Dataframe with Matplotlib?

AmitDiwan
Updated on 19-Oct-2021 08:50:42

7K+ Views

We will plot a line grapg for Pandas DataFrame using the plot(). At first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltCreate a DataFrame −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [2000, 2500, 2800, 3000, 3200, 3500], "Units": [100, 120, 150, 170, 180, 200] } )Plot a line graph with both the columns −plt.plot(dataFrame["Reg_Price"], dataFrame["Units"])ExampleFollowing is the code −import pandas as pd import matplotlib.pyplot as plt # creating a DataFrame with 2 columns dataFrame = pd.DataFrame(    {       "Car": ['BMW', ... Read More

Python - Plot a Pie Chart for Pandas Dataframe with Matplotlib?

AmitDiwan
Updated on 01-Oct-2021 11:14:16

12K+ Views

To plot a Pie Chart, use the plot.pie(). The pie plot is a proportional representation of the numerical data in a column.Import the required libraries −import pandas as pd import matplotlib.pyplot as pltCreate a DataFrame −dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] })Plot a Pie Chart for Registration Price column with label Car column −plt.pie(dataFrame["Reg_Price"], labels = dataFrame["Car"]) ExampleFollowing is the code −import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = pd.DataFrame({    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, ... Read More

Python - Plot a Histogram for Pandas Dataframe with Matplotlib?

AmitDiwan
Updated on 30-Sep-2021 13:23:49

2K+ Views

Histogram is a representation of the distribution of data. To plot a Histogram, use the hist() method. At first, import both the libraries −import pandas as pd import matplotlib.pyplot as pltCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame({    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] })Plot a Histogram for Registration Price column −plt.hist(dataFrame["Reg_Price"])ExampleFollowing is the code −import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = pd.DataFrame({    "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] }) # plot a ... Read More

Draw a curve connecting two points instead of a straight line in matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:38:05

4K+ Views

To draw a curve connecting two points instead of a straight line in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Define a draw_curve() method to make a curve with a mathematical expression.Plot point1 and point2 data points.Plot x and y data points returned from the draw_curve() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def draw_curve(p1, p2): a = (p2[1] - p1[1]) / (np.cosh(p2[0]) - np.cosh(p1[0])) b ... Read More

Plot data from a .txt file using matplotlib

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:28:07

4K+ Views

To plot data from .txt file using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize empty lists for bar_names and bar_heights.Open a sample .txt file in read "r" mode and append to bar's name and height list.Make a bar plot.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 bar_names = [] bar_heights = [] for line in open("test_data.txt", "r"): bar_name, bar_height = line.split() bar_names.append(bar_name) bar_heights.append(bar_height) plt.bar(bar_names, bar_heights) plt.show()"test_data.txt" contains the following data −Javed ... Read More

Advertisements