如何使用Matplotlib基于Numpy数组绘制多技术年份趋势图表
Got it, let's walk through exactly how to create that plot matching your example style. I'll assume you want a line plot (the most common choice for time-based trends) with each tech as a distinct line, clear labels, and the x-axis showing actual years instead of array indices.
Step 1: Import Required Libraries
First, make sure you have numpy and matplotlib installed, then import them into your script:
import numpy as np import matplotlib.pyplot as plt
Step 2: Organize Your Data & Years
You already have your NumPy arrays, so replace the sample values below with your actual data:
# Replace these with your real arrays! php = np.array([85, 80, 75, 70, 65, 60, 58, 55]) js = np.array([60, 65, 70, 78, 85, 90, 92, 95]) html = np.array([70, 72, 74, 76, 75, 73, 72, 70]) html5 = np.array([40, 50, 60, 70, 78, 85, 88, 90]) my_sql = np.array([75, 73, 70, 68, 65, 62, 60, 58]) # Create a years array to map indices to actual years (0=2015, 7=2022) years = np.arange(2015, 2023)
Step 3: Build the Plot with Example-Matching Styles
Here's the core code to generate the plot. Tweak colors, line styles, and markers to match your example exactly:
# Set figure size (adjust width/height to match your example's dimensions) plt.figure(figsize=(10, 6)) # Plot each tech with custom styling (adjust these to match your example) plt.plot(years, php, label='PHP', color='#777777', linestyle='-', marker='o') plt.plot(years, js, label='JavaScript', color='#f7df1e', linestyle='--', marker='s') plt.plot(years, html, label='HTML', color='#e34c26', linestyle='-.', marker='^') plt.plot(years, html5, label='HTML5', color='#e34c26', linestyle=':', marker='D') plt.plot(years, my_sql, label='MySQL', color='#00758f', linestyle='-', marker='*') # Add title and axis labels (customize text to match your example) plt.title('Tech Popularity Trends (2015-2022)', fontsize=14, fontweight='bold') plt.xlabel('Year', fontsize=12) plt.ylabel('Trend Value', fontsize=12) # Set x-axis ticks to display every year (instead of default auto-ticks) plt.xticks(years) # Add a legend to identify each line (adjust position if needed) plt.legend(loc='best') # Add a subtle grid for readability (common in example plots) plt.grid(True, alpha=0.3) # Adjust layout to prevent label cutoff plt.tight_layout() # Show the plot (or save it to a file with plt.savefig('tech_trends.png')) plt.show()
Key Customizations to Match Your Example
If your target plot has specific design elements, tweak these parts:
- Colors: Replace hex codes with exact colors from your example (use tools like a color picker to get the right values).
- Line Styles: Use
linestyle='-','--','-.':to match line patterns. - Markers: Choose markers like
'o','s','^'to replicate data point shapes. - Legend Position: Change
loc='best'to'upper left','lower right', etc., if your example places the legend elsewhere. - Text: Update the title and axis labels to match the exact wording in your example.
- Plot Type: If your example is a bar chart instead of a line plot, replace
plt.plot()withplt.bar()(you’ll need to offset bars for each tech to avoid overlap).
Content is sourced from Stack Exchange, question author Proger228

