Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to plot arbitrary markers on a Pandas data series using Matplotlib?
To plot arbitrary markers on a Pandas data series, we can use pyplot.plot() with custom markers and styling options. This is useful for visualizing time series data or any indexed data with distinctive markers.
Steps
- Set the figure size and adjust the padding between and around the subplots
- Create a Pandas data series with axis labels (including timeseries)
- Plot the series using plot() method with custom markers and line styles
- Use tick_params() method to rotate overlapping labels for better readability
- Display the figure using show() method
Example
Here's how to create a time series plot with star markers and dotted lines ?
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
ts = pd.Series(np.random.randn(10),
index=pd.date_range('2021-04-10', periods=10))
plt.plot(ts.index, ts, '*', ls='dotted', color='red')
plt.tick_params(rotation=45)
plt.show()
Different Marker Types
You can use various marker types for different visualization needs ?
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
# Create sample data
data = pd.Series([1, 4, 2, 8, 5, 7],
index=['A', 'B', 'C', 'D', 'E', 'F'])
plt.figure(figsize=(10, 6))
# Plot with different markers
plt.plot(data.index, data, 'o-', label='Circle', markersize=8)
plt.plot(data.index, data + 1, 's--', label='Square', markersize=8)
plt.plot(data.index, data + 2, '^:', label='Triangle', markersize=8)
plt.legend()
plt.title('Different Marker Types')
plt.grid(True, alpha=0.3)
plt.show()
Customizing Marker Properties
You can customize marker size, color, and edge properties for better visualization ?
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
# Create sample time series
ts = pd.Series(np.random.randn(8),
index=pd.date_range('2021-04-10', periods=8))
plt.figure(figsize=(10, 5))
plt.plot(ts.index, ts,
marker='D', # Diamond marker
markersize=10, # Marker size
markerfacecolor='lightblue', # Fill color
markeredgecolor='navy', # Edge color
markeredgewidth=2, # Edge width
linestyle='-', # Solid line
linewidth=2, # Line width
color='darkblue') # Line color
plt.title('Customized Markers and Lines')
plt.tick_params(rotation=45)
plt.grid(True, alpha=0.3)
plt.show()
Common Marker Symbols
| Symbol | Marker | Description |
|---|---|---|
| 'o' | ? | Circle |
| 's' | ? | Square |
| '^' | ? | Triangle up |
| '*' | ? | Star |
| 'D' | ? | Diamond |
Conclusion
Use plt.plot() with marker parameters to create visually appealing plots. Combine different markers, colors, and line styles to distinguish between multiple data series effectively.
Advertisements
