Python libraries
#Here's a simple Python program that uses the `matplotlib` library to create a pie chart for the sales distribution of different fruits in a store:
import matplotlib.pyplot as plt
# Sample data
fruits = ['Apples', 'Bananas', 'Cherries', 'Oranges', 'Strawberries']
sales = [30, 20, 25, 15, 10]
# Create a pie chart
plt.figure(figsize=(8, 8)) # Set the figure size
plt.pie(sales, labels=fruits, autopct='%1.1f%%')
plt.title('Sales Distribution of Different Fruits in a Store')
# Show the pie chart
plt.show()
2. Histogram
#The `hist()` function is used to create a histogram, which is a graphical representation of the distribution of numerical data. It groups the data into bins and counts the number of observations in each bin.
#### Example Code for `hist()`:
import matplotlib.pyplot as plt
import numpy as np
# Generate random data
data = np.random.randn(1000) # 1000 random numbers from a normal distribution
# Create a histogram
plt.hist(data, bins=30, color='green', alpha=0.7)
# Adding labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
# Show the histogram
plt.show()
3. Bar chart
#The `bar()` function is used to create a bar chart, which is a way of representing categorical data with rectangular bars. The height of each bar represents the value of the corresponding category.
#### Example Code for `bar()`:
import matplotlib.pyplot as plt
# Data for the bar chart
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 5, 2]
# Create a bar chart
plt.bar(categories, values, color='blue')
# Adding labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart Example')
# Show the chart
plt.show()
4. Scatter plot
#Scatter plots display individual data points and are commonly used to observe relationships or trends between two variables.
#plt.scatter(x, y, color='optional_color', marker='optional_marker')
#• x and y: Coordinates of the points.
#• color: Optional; specifies the color of the points.
#• marker: Optional; defines the style of the markers (e.g., 'o', 'x', '^').
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
plt.scatter(x, y, color='green', marker='o')
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
5. Line plot
#Here's a simple Python program that uses Matplotlib to draw a line graph with the specified data and axis labels
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
# Create a line graph
plt.plot(x, y, marker='o')
# Adding labels
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Adding a title
plt.title('Line Graph Example')
# Show the graph
plt.grid()
plt.show()
### Explanation:
'''- The program uses the `matplotlib.pyplot` library to create a line graph.
- It defines the x and y data points, then plots them using `plt.plot()`.
- The `xlabel()` and `ylabel()` functions are used to label the axes.
- Finally, it displays the graph using `plt.show()`. '
Comments
Post a Comment