[PYTHON] How to apply markers only to specific data in matplotlib

Introduction

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. max_2020.png ↑ I wanted to apply a marker only at a specific value to emphasize it like this.

environment

$ 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

problem

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. before.png

What if you want to mark only the January, April, July, and October data in this graph with diamonds (and not others)?

Solution

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 markeevery1.png

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 markeevery2.png

case being settled.

References

Markevery Demo — Matplotlib 3.1.2 documentation

Recommended Posts

How to apply markers only to specific data in matplotlib
How to suppress display error in matplotlib
How to create data to put in CNN (Chainer)
How to read time series data in PyTorch
[Python] How to draw a histogram in Matplotlib
How to count numbers in a specific range
How to use Matplotlib
How to eliminate garbled characters in matplotlib output image
How to run setUp only once in python unittest
How to add color bars in matplotlib bar plot
How to pass matplotlib backend settings in environment variables
How to read csv containing only integers in Python
How to develop in Python
How to handle data frames
How to generate exponential pulse time series data in python
How to add new data (lines and plots) using matplotlib
How to get an overview of your data in Pandas
Simultaneously input specific data to a specific sheet in many excels
Data science companion in python, how to specify elements in pandas
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
How to read e-Stat subregion data
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use SQLite in Python
How to deal with imbalanced data
How to deal with imbalanced data
How to run matplotlib on heroku
How to convert 0.5 to 1056964608 in one shot
Try to put data in MongoDB
<Pandas> How to handle time series data in a pivot table
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to Data Augmentation with PyTorch
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to create a large amount of test data in MySQL? ??
How to visualize where misclassification is occurring in data analysis classification
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to collect machine learning data
How to call PyTorch in Julia
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to get a specific column name and index name in pandas DataFrame
How to send a visualization image of data created in Python to Typetalk
How to make a process thread run only on a specific CPU core
How to store CSV data in Amazon Kinesis Streams with standard input
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
How to plot galaxy visible light data using OpenNGC database in python
How to find a specific type (str, float etc) column in a DataFrame column
Try to extract specific data from JSON format data in object storage Cloudian/S3
How to make only one data register on the Django admin screen
How to stop a program in python until a specific date and time
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to collect Twitter data without programming