Found 784 Articles for Data Visualization

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

How to save a histogram plot in Python?

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:13:15

6K+ Views

To save a histogram plot in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create data points "k" for the histogram.Plot the histogram using hist() method.To save the histogram, use plt.savefig('image_name').To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Data points for the histogram k = [1, 3, 2, 5, 4, 7, 5, 1, 0, 4, 1] # Plot the histogram plt.hist(k) # Save the histogram plt.savefig('hist.png') # Display the ... Read More

Correlation between two numeric columns in a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:10:41

948 Views

We can use pandas.DataFrame.corr to compute pairwise correlation of columns, excluding NULL values. The correlation coefficient indicates the strength of the linear association between two variables. The coefficient ranges between -1 and 1.To get the correlation between two numeric columns in a Pandas dataframe, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.Compare the values of the two columns and compute the correlation coefficient using col1.corr(col2).Print the correlation coefficient on the console.To display the figure, use show() method.Exampleimport pandas as ... Read More

Advertisements