2016-11-20 111 views
-1

我有2個文件數據,我試圖使用matplotlib創建一個數字:縮放地塊matplotlib

data1.csv:

T1,T2,Energy 
-10.0,-10.0,-135.17209154 
-9.0,-9.0,-135.15961763 
-8.0,-8.0,-135.12933854 
-7.0,-7.0,-135.08764644 
-6.0,-6.0,-135.04017015 
-5.0,-5.0,-134.9917753 
-4.0,-4.0,-134.9465778 
-3.0,-3.0,-134.90793353 
-2.0,-2.0,-134.8782808 
-1.0,-1.0,-134.86001462 
0.0,0.0,-134.85401885 
1.0,1.0,-134.86001461 
2.0,2.0,-134.87828068 
3.0,3.0,-134.90793366 
4.0,4.0,-134.94657765 
5.0,5.0,-134.99177495 
6.0,6.0,-135.04017025 
7.0,7.0,-135.08764629 
8.0,8.0,-135.12933843 
9.0,9.0,-135.15961751 
10.0,10.0,-135.17209131 

data2.csv:

T1,T2,Energy 
-10.0,-10.0,-117.71068713 
-9.0,-9.0,-117.80680053 
-8.0,-8.0,-117.87570934 
-7.0,-7.0,-117.92318137 
-6.0,-6.0,-117.95416633 
-5.0,-5.0,-117.97304758 
-4.0,-4.0,-117.98349172 
-3.0,-3.0,-117.9884913 
-2.0,-2.0,-117.99017179 
-1.0,-1.0,-117.99078073 
0.0,0.0,-117.99109191 
1.0,1.0,-117.99083075 
2.0,2.0,-117.99029791 
3.0,3.0,-117.98887774 
4.0,4.0,-117.98440979 
5.0,5.0,-117.97484573 
6.0,6.0,-117.95727936 
7.0,7.0,-117.92813366 
8.0,8.0,-117.88311587 
9.0,9.0,-117.81736874 
10.0,10.0,-117.72521723 

我寫的腳本:

!/usr/bin/env python 
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

FRAME1 = pd.read_csv('data1.csv') 
FRAME2 = pd.read_csv('data2.csv') 

FRAME1.columns = ['T1', 'T2', 'ENERGY'] 
FRAME2.columns = ['T1', 'T2', 'ENERGY'] 

FRAME1 = FRAME1.iloc[::-1] 
FRAME2 = FRAME2.iloc[::-1] 


plt.plot(FRAME1['T1'],FRAME1['ENERGY'], marker = 'o', label='1') 
plt.plot(FRAME2['T1'],FRAME2['ENERGY'], marker = 'o', label='2') 
plt.legend(loc='upper right') 
plt.ylim = np.arange(-140.854019, -118.991092, 0.002) 

plt.xlabel(r'$\Delta T$') 
plt.ylabel('Eint') 
plt.grid(True) 
plt.savefig("test.png") 
plt.show() 

但是我與建立正確的規模鬥爭。 enter image description here

我的情節似乎幾乎是線性的。我想改變一個比例尺,並以真實的拋物線形狀顯示它們。

+0

你可以改變比例' plt.ylim',但如果你想在同一張圖上繪製兩張圖,你的縮放比例將與圖片上顯示的相似(因爲y的最小值接近-135,最大接近-117)。您可以爲'ylim'分配不同的值,但最有可能之後只有一張圖將出現在圖片上。你喜歡什麼樣的身材? –

回答

2

您可以使用twinx()軸方法,如下所示:

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import FormatStrFormatter 

FRAME1 = pd.read_csv('data1.csv') 
FRAME2 = pd.read_csv('data2.csv') 

FRAME1.columns = ['T1', 'T2', 'ENERGY'] 
FRAME2.columns = ['T1', 'T2', 'ENERGY'] 

FRAME1 = FRAME1.iloc[::-1] 
FRAME2 = FRAME2.iloc[::-1] 

fig = plt.figure(figsize=(10,6)) 
ax1 = fig.add_subplot(111) 

h1, = ax1.plot(FRAME1['T1'], FRAME1['ENERGY'], color="b", marker = 'o', label='1') 
ax2 = ax1.twinx() 
h2, = ax2.plot(FRAME2['T1'],FRAME2['ENERGY'], color="g", marker = 'o', label='2') 

ax1.set_xlabel(r'$\Delta T$') 
ax1.set_ylabel('Eint') 
ax2.set_ylabel('Eint') 
ax1.yaxis.set_major_formatter(FormatStrFormatter('%0.2f')) 
ax2.yaxis.set_major_formatter(FormatStrFormatter('%0.2f')) 
ax1.grid(True) 
ax2.grid(True) 
labels = [lb.get_label() for lb in (h1,h2)] 
ax1.legend((h1,h2), labels) 

plt.savefig("test.png") 
plt.show() 

,並獲得:

欲瞭解更多信息檢查this example約twinx