In When graphing the publication order of magazines, I wanted to add markers (dots to the graph) only to specific items with matplotlib (center). I wanted to put a mark so that it would be easy to understand only when it was in color), but I did not find it at all when I searched for it, so make a note. ↑ I wanted to apply a marker only at a specific value to emphasize it like this.
$ uname -a
Linux kali 4.18.0-kali2-amd64 #1 SMP Debian 4.18.10-2kali1 (2018-10-09) x86_64 GNU/Linux
$ python3 --version
Python 3.7.6
$ pip3 show matplotlib
Name: matplotlib
Version: 3.1.2
For example, suppose you have the following data.
month | January | February | March | April | May | June | July | August | September | October | 1January | 1February |
---|---|---|---|---|---|---|---|---|---|---|---|---|
data | 13 | 15 | 21 | 5 | 10 | 18 | 21 | 17 | 15 | 16 | 21 | 13 |
This is a graph of these.
What if you want to mark only the January, April, July, and October data in this graph with diamonds (and not others)?
When plotting only the part to be marked as an array, pass it with `` mark every appropriately
.
If you want to add the Xth marker, add the X-1st number to the array.
Like this.
#!/usr/bin/env python
import matplotlib.pyplot as plt
X_data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Y_data=[13,15,21,5,10,18,21,17,15,16,21,13]
month_name=['Jan.','Feb.','Mar.','Apr.','May','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.']
mark_point=[0,3,6,9]
plt.xlabel('month')
plt.ylabel('data')
plt.grid(color='gray')
plt.xticks(X_data,month_name)
plt.yticks(range(1,max(Y_data)+1))
plt.plot(X_data,Y_data, '.', linestyle='solid', marker="D", markevery=mark_point)
plt.show()
Results
If you want to use the y-axis data as a reference, you can compare the data first and store it in the array. Example) Apply markers only when the data is odd
#!/usr/bin/env python
import matplotlib.pyplot as plt
X_data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Y_data=[13,15,21,5,10,18,21,17,15,16,21,13]
month_name=['Jan.','Feb.','Mar.','Apr.','May','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.']
mark_point=[]
for i,data in enumerate(Y_data):
if data%2:
mark_point.append(i)
plt.xlabel('month')
plt.ylabel('data')
plt.grid(color='gray')
plt.xticks(X_data,month_name)
plt.yticks(range(1,max(Y_data)+1))
plt.plot(X_data,Y_data, '.', linestyle='solid', marker="D", markevery=mark_point)
plt.show()
Results
case being settled.
Markevery Demo — Matplotlib 3.1.2 documentation
Recommended Posts