0

I wanted to use xticks function 2 times on the x axis for the 2 plots but it doesn't work. I wanted to know a way to use the xticks tool to display my second axis on the top :

my_xticks2 = ['524K','574K','Interpolation \n entre 524 et 574K','Melange stochastique \n entre 524 et 574K']

import numpy  as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.ticker import StrMethodFormatter

data = np.loadtxt('k_inf')
data2=np.loadtxt('k_inf-jeff33')

x = data[:, 0]
y = data[:, 1]
yerr=data[:, 2]

x2 = data2[:, 0]
y2 = data2[:, 1]
yerr2=data2[:, 2]

plt.errorbar(x, y, yerr, fmt='o', label='ENDF/B-VIII')
plt.errorbar(x2, y2, yerr2, fmt='o', label='JEFF-3.3')

my_xticks = ['550K','574K','Interpolation \n entre 550 et 574K','Melange stochastique \n entre 550 et 574K']
plt.xticks(x, my_xticks)

my_xticks2 = ['524K','574K','Interpolation \n entre 524 et 574K','Melange stochastique \n entre 524 et 574K']
plt.xticks(x2, my_xticks2)

plt.legend()

plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.5f}')) # 5 decimal places
plt.ylabel('k_inf')
plt.title('plot avec ENDF/B-VIII et JEF3.3')

plt.savefig('JEF3_and_ENDFB8.pdf', dpi=300, bbox_inches='tight')

plt.show()
  • 1
    Something like `ax = plt.gca()`; `ax2 = ax.twiny()`; `ax2.set_xticks(ax.get_xticks())`; `ax2.set_xticklabels(my_xticks2)` might work. Note that your post is missing test data. – JohanC Mar 25 '22 at 20:27
  • Hello thank you for your answer, but i don't find options to upload files. I tested your solutions, it displays well the second axis but the graduation doesn't follow the actual points on the plots contrary to the axis my_sticks which works good. – haswellrefresh Mar 26 '22 at 19:53
  • Test data are meant to be added as text to the post. Please don't upload the whole set, only a small set of data similar to what you have, and that [reproduces](https://stackoverflow.com/help/minimal-reproducible-example) the issue. Also, an [image](https://meta.stackoverflow.com/questions/344851/how-do-you-add-a-screenshot-image-to-your-stack-overflow-post) of your last plot might be helpful to understand what's going on. Note that on StackOverflow, you are supposed to [edit](https://stackoverflow.com/posts/71621940/edit) your post to add your latest insights. – JohanC Mar 26 '22 at 21:36
  • Maybe `ax2.set_xlim(ax.get_xlim())` could be added to make sure both axes are well-aligned? – JohanC Mar 26 '22 at 21:38
  • finally it works, it's just the pyzo display that is bad. At work i could use jupyter it works well, thank you anyway. – haswellrefresh Mar 28 '22 at 18:29

0 Answers0