2017-08-23 15 views
0

こんにちは私はx軸にM値、y軸にN値を持つ基本的なコードをいくつか持っています。Python:pandasとxlsxwriterを使用してExcelで散布図を作成したい

私はmatplotlibを使用して得たグラフをMicrosoft Excelで作成することができます。これは可能ですか?

私は試しました:chart = workbook.add_chart({'type': 'scatter'}) 成功しませんでした。私がmatplotlibで取得したものと同様のチャートを得ることができる代替の 'タイプ'がありますか?あなたはほとんどそれを持って

M = [-2.29, 33.63, 66.52, 96.37, 123.10, 146.79, 167.49, 185.20, 199.94, 211.68, 220.44, 226.22, 229.02, 230.25, 227.07, 220.91, 211.77, 199.64, 184.53, 164.48, 141.67, 0.00] 
N = [-292.20, 6.65, 305.47, 604.25, 901.94, 1198.81, 1495.67, 1792.54, 2089.41, 2386.27, 2683.14, 2980.01, 3276.88, 3561.89, 3858.76, 4155.63, 4452.49, 4749.36, 5046.23, 5361.80, 5666.67, 5666.67] 


import pandas as pd 

# Create a Pandas dataframe from some data for M. 
df1 = pd.DataFrame({'M (kNm)': [M[0], M[1], M[2], 
M[3], M[4], M[5], M[6], M[7], 
    M[8], M[9], M[10], M[11], M[12], 
    M[13], M[14], M[15], M[16], M[17], 
    M[18], M[19], M[20], M[21]]}) 

# Create a Pandas dataframe from some data for N. 
df2 = pd.DataFrame({'N (kN)': [N[0], N[1], N[2], 
N[3], N[4], N[5], N[6], N[7], 
    N[8], N[9], N[10], N[11], N[12], 
    N[13], N[14], N[15], N[16], N[17], 
    N[18], N[19], N[20], N[21]]}) 

# Create a Pandas Excel writer using XlsxWriter as the engine. 
writer = pd.ExcelWriter('MN_example.xlsx', engine='xlsxwriter') 

# Convert the dataframe to an XlsxWriter Excel object. 
df1.to_excel(writer, sheet_name='Sheet1', index=False) 
df2.to_excel(writer, sheet_name='Sheet1', startcol=1, index=False) 

# Get the xlsxwriter workbook and worksheet objects. 
workbook = writer.book 
worksheet = writer.sheets['Sheet1'] 

# Create a chart object. 
chart = workbook.add_chart({'type': 'scatter'}) 

# Configure the series of the chart from the dataframe data. 
chart.add_series({'values': '=Sheet1!$A$2:$B$23'}) 

# Insert the chart into the worksheet. 
worksheet.insert_chart('D2', chart) 

# Close the Pandas Excel writer and output the Excel file. 
writer.save() 

import matplotlib.pyplot as plt 

plt.plot(M, N, linewidth=2) 


plt.show() 
+0

これが可能に重複することができ.com/questions/15177705/can-i-insert-matplotlib-graphs-into-programmatically/15177991#15177991 – tom

+0

こんにちは。基本的には、チャートをExcel内で「スムーズな線を持つ散布図」として作成したいと考えています。 – dazedandconfused

+0

だから私はちょうど重複を提案し、それを閉じなかったのです:) – tom

答えて

0

、あなただけのチャートsubtypeを指定してもデータを指定する必要がcategoriesチャート上にプロットされるべきであること。

以下でチャートの初期化と設定を元に戻し、自分の欲しいものを手に入れる必要があります。

# Create a chart object. 
chart = workbook.add_chart({'type': 'scatter', 
          'subtype': 'smooth'}) 

# Configure the series of the chart from the dataframe data. 
chart.add_series({ 
    'categories': ['Sheet1', 1, 0, len(df2), 0], 
    'values':  ['Sheet1', 1, 1, len(df2), 1]}) 

は出力します。https:// stackoverflowの

enter image description here

+0

素晴らしい!私は実際にそれを使用して動作するようにしました: – dazedandconfused

+0

#グラフオブジェクトを作成します。 chart = workbook.add_chart({type ':' scatter '、 'サブタイプ ':' smooth '}) #最初のシリーズを設定します。 chart.add_series({ 'name': '= Sheet1!$ A $ 1'、 'カテゴリ': '= Sheet1!$ A $ 2:$ A $ 23'、 '値': '= Sheet1!$ B $ 2 :$ B $ 23 '、 }) – dazedandconfused

+0

あなたのコードははるかにきれいです! – dazedandconfused

関連する問題