0

I need to put minor ticks in the x axis in subplots. This is my script, What I need to change in it?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from matplotlib import rc
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import matplotlib.ticker as tkr
from pylab import text
import matplotlib.patches as mpatches
import matplotlib.font_manager as font_manager
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter)

  
a = open("file1.txt").read().splitlines()
b = open("file2.txt").read().splitlines()
c = open("file3.txt").read().splitlines()

variable1 = np.ravel(a).astype(np.float)
variable2 = np.ravel(b).astype(np.float)
variable3 = np.ravel(c).astype(np.float)

fig, ax = plt.subplots(figsize=(15,30))
#fig, ax = plt.subplots(figsize=(14,10))
plt.subplots_adjust(wspace=0.08, hspace=0.05)

font_path = '/Library/Fonts/Helvetica.ttf'
font_prop = font_manager.FontProperties(fname=font_path, size=20)

plt.subplot(311)
plt.xticks(size=20)
plt.yticks(size=20)
plt.hist(variable1, bins=100, histtype='bar', density=True, ec='red', color='red', alpha=0.5)
plt.grid()
plt.tight_layout()
plt.ylim(0,90)
plt.yticks(np.arange(0, 100, 10))
plt.xlim(-0.02,0.5)
plt.xticks(np.arange(0, 0.5, 0.05))
ax.xaxis.set_major_locator(MultipleLocator(0.5))
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
ax.xaxis.set_minor_locator(MultipleLocator(5))
ax.minorticks_on()

plt.show()

I'm trying put ticks minor in the first subplot as a first try. Any can help me and support me please? Thanks a lot

Hdez
  • 151
  • 7
  • You might want to check out [matplotlib Axes.plot() vs pyplot.plot()](https://stackoverflow.com/questions/43482191/matplotlib-axes-plot-vs-pyplot-plot) and [The Lifecycle of a Plot](https://matplotlib.org/3.3.1/tutorials/introductory/lifecycle.html). You shouldn't be using `plt.subplot(311)` and `fig, ax = plt.subplots(figsize=(15,30))` in the same code, but make a choice between them (`fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(15,30))` would be the recommended way nowadays). – JohanC Sep 23 '20 at 20:23
  • Thanks a lot JohanC, I'm learning python and your advice will help me.! I think that my script have a lot of improvement points. Do you have any idea how can I print minor ticks in the x axis? Thanks – Hdez Sep 23 '20 at 20:53
  • Well, you really should edit your question, remove the `plt.subplot(311)` and work with the axes. – JohanC Sep 24 '20 at 04:43

0 Answers0