[PYTHON] matplotlib: Replace the axis itself with another one.

It can be executed with python2.6 +matplotlib (1.4.3)[^ 1].

[^ 1]: Cannot be executed with matplotlib (1.0.1)

When you want to make the axis a completely different scale when creating a graph.

--From real numbers to time axis on the X axis. --Set the Y axis from 0 to 10 to 0 to 100

This time, in the subplot, after creating the axis, plot the data for ʻax1.twinx (). Twiny () . The plot target is rand.txt`, which I use every time.

python


$ perl -le 'print rand (10) for 0 .. 99' > rand.txt

replace.py


#!/usr/bin/env python -B
# -*- coding: utf-8 -*-
import sys
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv(sys.argv[1], header=None)
#Axis plot
fig, ax1 = plt.subplots()
ax1.xaxis_date()         #X is time series
ax1.set_ylim([0,100])    #Y is 0~100
ax1.grid(True)
ax = ax1.twinx().twiny()

#Data plot
df.plot(ax=ax,xticks=[],yticks=[])
ax.legend().set_visible(False) 
ax.grid(False)
fig.subplots_adjust( bottom=0.15, top=0.95,left=0.1, right=0.95)
plt.savefig(sys.argv[2])

python


$ python replace.py rand.txt graph.png

change.png

background

When dealing with time series with matplotlib, plotting data [^ 2] containing a huge amount of time stamps seems to rot around locators such as dates.MonthLocator.

[^ 2]: About 10 * 365 * 24 * 60 (hour order).

If you execute it without arguments, it will be a decent picture, but once (once), if you execute it with interval = 4, etc., the grid and xticklabel will be messed up.

In order to avoid that, subtract a huge amount of time stamps appropriately and draw a grid etc. from there [^ 3].

[^ 3]: 10 * 365 * 24 (daily order)

memo

--At the end, do fig.subplots_adjust (). --Plots with pandas may change the image width without permission, so make a habit of explicitly adjusting.

Recommended Posts

matplotlib: Replace the axis itself with another one.
2-axis plot with Matplotlib
[Python] Set the graph range with matplotlib
Adjust the spacing between figures with Matplotlib
Align the size of the colorbar with matplotlib
Set the xticklabels color individually with matplotlib
Draw hierarchical axis labels with matplotlib + pandas
Display markers above the border with matplotlib
[Matplotlib] Do not tilt the axis label
Match the colorbar to the figure with matplotlib
[Python] limit axis of 3D graph with Matplotlib
Drawing tips with matplotlib on the server side
Increase the font size of the graph with matplotlib
One liner that lists the colors of matplotlib
Follow the AR marker with a 2-axis servo
Put the second axis in 2dhistgram of matplotlib
The basis of graph theory with matplotlib animation
Visualize the behavior of the sorting algorithm with matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
Load the module with the same name in another location
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
Try to specify the axis with PyTorch's Softmax function