If you’re scratching your head wondering why you’re getting a blank plot when attempting to compare customer order data in Pandas, you’re not alone. Let’s dive into the issue using the provided example dataset and explore a solution to rectify the blank plot problem.
1. Understanding the Scenario.
- Consider a scenario where you have customer order data presented in the following format:
Customer No Customer Type Order Time Order Amount Order Quantity 0 126 Regular 2023-01-01 00:00:00.000000000 279.330970 6 1 126 Regular 2023-01-01 04:24:29.387755102 75.071612 4 2 126 Regular 2023-01-01 08:48:58.775510204 253.021647 2 3 126 Regular 2023-01-01 13:13:28.163265306 58.994449 9 4 126 Regular 2023-01-01 17:37:57.551020408 248.769915 1 5 126 Regular 2023-01-01 22:02:26.938775510 490.814028 5 6 126 Regular 2023-01-02 02:26:56.326530612 211.750009 7 7 126 Regular 2023-01-02 06:51:25.714285714 266.402089 6 8 126 Regular 2023-01-02 11:15:55.102040816 359.897532 8 9 126 Regular 2023-01-02 15:40:24.489795918 446.214150 9 10 186 Regular 2023-01-02 20:04:53.877551020 463.205960 9 11 186 Regular 2023-01-03 00:29:23.265306122 147.569962 3 12 186 Regular 2023-01-03 04:53:52.653061224 304.334990 9 13 186 Regular 2023-01-03 09:18:22.040816326 439.296153 7 14 186 Regular 2023-01-03 13:42:51.428571428 279.036032 7
- The objective is to compare the data for each ‘Customer No‘ against the most recent information within a specified time range.
- The goal is to plot the ‘Order Quantity‘ against ‘Order Time‘ for each customer separately.
2. The Code and the Blank Plot Issue.
- You’ve crafted a code snippet to achieve this objective, but upon execution, you notice that it returns a blank plot.
- Let’s examine the original code:
import pandas as pd import matplotlib.pyplot as plt import numpy as np # Generate random data for the example dataset np.random.seed(0) num_customers = 5 num_orders_per_customer = 10 customer_nos = np.random.choice(range(100, 200), size=num_customers, replace=False) customer_types = np.random.choice(['Regular', 'Non Regular'], size=num_customers) order_times = pd.date_range(start='2023-01-01', end='2023-01-10', periods=num_customers*num_orders_per_customer) order_amounts = np.random.uniform(50, 500, size=num_customers*num_orders_per_customer) order_quantities = np.random.randint(1, 10, size=num_customers*num_orders_per_customer) # Create the DataFrame data = { 'Customer No': np.repeat(customer_nos, num_orders_per_customer), 'Customer Type': np.repeat(customer_types, num_orders_per_customer), 'Order Time': order_times, 'Order Amount': order_amounts, 'Order Quantity': order_quantities } df = pd.DataFrame(data) print("Original df: ") print(df) print("******************************************************") # Define a plotting function def plot_customer(x): plt.plot(x['Order Time'], x['Order Amount'])#, marker='o') plt.xlabel('Order Time') plt.ylabel('Order Amount') plt.title('Order Amount vs Order Time for Customer No: ' + str(x['Customer No'].iloc[0])) plt.gcf().autofmt_xdate() plt.ylim(0, x['Order Amount'].max() * 1.1) plt.show() # Group the DataFrame by 'Customer No' grouped_df = df.groupby('Customer No') # Iterate over groups and print each group for name, group in grouped_df: print(f"Customer No: {name}") print(group) print("\n") # Add a new line for better readability print("******************************************************") # Apply the plotting function to each group grouped_df.apply(plot_customer)
- The issue stems from the absence of markers in the plot. Without markers, the plot may not display any visible data points, resulting in a seemingly blank plot.
3. The Solution.
- To rectify this, add the ‘marker‘ parameter to the `plt.plot` function within the plotting function:
plt.plot(x['Order Time'], x['Order Amount'], marker='o') # Add marker parameter
- Now, with markers added, you should see your customer order data points plotted over time.
- With these adjustments, the blank plot issue should be resolved, and your customer order data will be accurately visualized over time.
- When you run the above code example, it will generate the below output in the console.
Original df: Customer No Customer Type Order Time Order Amount Order Quantity 0 126 Regular 2023-01-01 00:00:00.000000000 279.330970 6 1 126 Regular 2023-01-01 04:24:29.387755102 75.071612 4 2 126 Regular 2023-01-01 08:48:58.775510204 253.021647 2 3 126 Regular 2023-01-01 13:13:28.163265306 58.994449 9 4 126 Regular 2023-01-01 17:37:57.551020408 248.769915 1 5 126 Regular 2023-01-01 22:02:26.938775510 490.814028 5 6 126 Regular 2023-01-02 02:26:56.326530612 211.750009 7 7 126 Regular 2023-01-02 06:51:25.714285714 266.402089 6 8 126 Regular 2023-01-02 11:15:55.102040816 359.897532 8 9 126 Regular 2023-01-02 15:40:24.489795918 446.214150 9 10 186 Regular 2023-01-02 20:04:53.877551020 463.205960 9 11 186 Regular 2023-01-03 00:29:23.265306122 147.569962 3 12 186 Regular 2023-01-03 04:53:52.653061224 304.334990 9 13 186 Regular 2023-01-03 09:18:22.040816326 439.296153 7 14 186 Regular 2023-01-03 13:42:51.428571428 279.036032 7 15 186 Regular 2023-01-03 18:07:20.816326530 462.525329 2 16 186 Regular 2023-01-03 22:31:50.204081632 464.520925 7 17 186 Regular 2023-01-04 02:56:19.591836734 87.400622 9 18 186 Regular 2023-01-04 07:20:48.979591836 174.973353 9 19 186 Regular 2023-01-04 11:45:18.367346938 54.210517 4 20 102 Non Regular 2023-01-04 16:09:47.755102040 429.053936 3 21 102 Non Regular 2023-01-04 20:34:17.142857142 341.228363 4 22 102 Non Regular 2023-01-05 00:58:46.530612244 428.623754 7 23 102 Non Regular 2023-01-05 05:23:15.918367346 169.128574 4 24 102 Non Regular 2023-01-05 09:47:45.306122449 229.019339 7 25 102 Non Regular 2023-01-05 14:12:14.693877551 298.769666 6 26 102 Non Regular 2023-01-05 18:36:44.081632653 124.223207 8 27 102 Non Regular 2023-01-05 23:01:13.469387755 216.413642 1 28 102 Non Regular 2023-01-06 03:25:42.857142857 115.898793 9 29 102 Non Regular 2023-01-06 07:50:12.244897959 306.328283 5 30 155 Non Regular 2023-01-06 12:14:41.632653061 366.681776 7 31 155 Non Regular 2023-01-06 16:39:11.020408163 179.814397 6 32 155 Non Regular 2023-01-06 21:03:40.408163265 244.979628 9 33 155 Non Regular 2023-01-07 01:28:09.795918367 390.248012 3 34 155 Non Regular 2023-01-07 05:52:39.183673469 228.244224 4 35 155 Non Regular 2023-01-07 10:17:08.571428571 453.217274 8 36 155 Non Regular 2023-01-07 14:41:37.959183673 337.514484 6 37 155 Non Regular 2023-01-07 19:06:07.346938775 451.199497 4 38 155 Non Regular 2023-01-07 23:30:36.734693877 356.025006 5 39 155 Non Regular 2023-01-08 03:55:06.122448979 252.138982 6 40 175 Regular 2023-01-08 08:19:35.510204081 490.356918 4 41 175 Regular 2023-01-08 12:44:04.897959183 102.290859 4 42 175 Regular 2023-01-08 17:08:34.285714285 395.160667 8 43 175 Regular 2023-01-08 21:33:03.673469387 235.319063 8 44 175 Regular 2023-01-09 01:57:33.061224489 353.947587 4 45 175 Regular 2023-01-09 06:22:02.448979591 162.408324 3 46 175 Regular 2023-01-09 10:46:31.836734693 190.948249 4 47 175 Regular 2023-01-09 15:11:01.224489795 484.437299 8 48 175 Regular 2023-01-09 19:35:30.612244898 314.809288 8 49 175 Regular 2023-01-10 00:00:00.000000000 346.850785 6 ****************************************************** Customer No: 102 Customer No Customer Type Order Time Order Amount Order Quantity 20 102 Non Regular 2023-01-04 16:09:47.755102040 429.053936 3 21 102 Non Regular 2023-01-04 20:34:17.142857142 341.228363 4 22 102 Non Regular 2023-01-05 00:58:46.530612244 428.623754 7 23 102 Non Regular 2023-01-05 05:23:15.918367346 169.128574 4 24 102 Non Regular 2023-01-05 09:47:45.306122449 229.019339 7 25 102 Non Regular 2023-01-05 14:12:14.693877551 298.769666 6 26 102 Non Regular 2023-01-05 18:36:44.081632653 124.223207 8 27 102 Non Regular 2023-01-05 23:01:13.469387755 216.413642 1 28 102 Non Regular 2023-01-06 03:25:42.857142857 115.898793 9 29 102 Non Regular 2023-01-06 07:50:12.244897959 306.328283 5 Customer No: 126 Customer No Customer Type Order Time Order Amount Order Quantity 0 126 Regular 2023-01-01 00:00:00.000000000 279.330970 6 1 126 Regular 2023-01-01 04:24:29.387755102 75.071612 4 2 126 Regular 2023-01-01 08:48:58.775510204 253.021647 2 3 126 Regular 2023-01-01 13:13:28.163265306 58.994449 9 4 126 Regular 2023-01-01 17:37:57.551020408 248.769915 1 5 126 Regular 2023-01-01 22:02:26.938775510 490.814028 5 6 126 Regular 2023-01-02 02:26:56.326530612 211.750009 7 7 126 Regular 2023-01-02 06:51:25.714285714 266.402089 6 8 126 Regular 2023-01-02 11:15:55.102040816 359.897532 8 9 126 Regular 2023-01-02 15:40:24.489795918 446.214150 9 Customer No: 155 Customer No Customer Type Order Time Order Amount Order Quantity 30 155 Non Regular 2023-01-06 12:14:41.632653061 366.681776 7 31 155 Non Regular 2023-01-06 16:39:11.020408163 179.814397 6 32 155 Non Regular 2023-01-06 21:03:40.408163265 244.979628 9 33 155 Non Regular 2023-01-07 01:28:09.795918367 390.248012 3 34 155 Non Regular 2023-01-07 05:52:39.183673469 228.244224 4 35 155 Non Regular 2023-01-07 10:17:08.571428571 453.217274 8 36 155 Non Regular 2023-01-07 14:41:37.959183673 337.514484 6 37 155 Non Regular 2023-01-07 19:06:07.346938775 451.199497 4 38 155 Non Regular 2023-01-07 23:30:36.734693877 356.025006 5 39 155 Non Regular 2023-01-08 03:55:06.122448979 252.138982 6 Customer No: 175 Customer No Customer Type Order Time Order Amount Order Quantity 40 175 Regular 2023-01-08 08:19:35.510204081 490.356918 4 41 175 Regular 2023-01-08 12:44:04.897959183 102.290859 4 42 175 Regular 2023-01-08 17:08:34.285714285 395.160667 8 43 175 Regular 2023-01-08 21:33:03.673469387 235.319063 8 44 175 Regular 2023-01-09 01:57:33.061224489 353.947587 4 45 175 Regular 2023-01-09 06:22:02.448979591 162.408324 3 46 175 Regular 2023-01-09 10:46:31.836734693 190.948249 4 47 175 Regular 2023-01-09 15:11:01.224489795 484.437299 8 48 175 Regular 2023-01-09 19:35:30.612244898 314.809288 8 49 175 Regular 2023-01-10 00:00:00.000000000 346.850785 6 Customer No: 186 Customer No Customer Type Order Time Order Amount Order Quantity 10 186 Regular 2023-01-02 20:04:53.877551020 463.205960 9 11 186 Regular 2023-01-03 00:29:23.265306122 147.569962 3 12 186 Regular 2023-01-03 04:53:52.653061224 304.334990 9 13 186 Regular 2023-01-03 09:18:22.040816326 439.296153 7 14 186 Regular 2023-01-03 13:42:51.428571428 279.036032 7 15 186 Regular 2023-01-03 18:07:20.816326530 462.525329 2 16 186 Regular 2023-01-03 22:31:50.204081632 464.520925 7 17 186 Regular 2023-01-04 02:56:19.591836734 87.400622 9 18 186 Regular 2023-01-04 07:20:48.979591836 174.973353 9 19 186 Regular 2023-01-04 11:45:18.367346938 54.210517 4 ******************************************************