質問編集履歴

2

プライバシーにかかわる部分を修正しました

2023/03/23 03:22

投稿

squere
squere

スコア2

test CHANGED
File without changes
test CHANGED
@@ -125,15 +125,15 @@
125
125
  from copy import deepcopy
126
126
 
127
127
  #使うエクセルファイル、シートを指定する。ワークシートの名前も指定する。
128
- wb = load_workbook('1945_matome.xlsx')
128
+ wb = load_workbook('matome.xlsx')
129
- ws = wb['SP_s1945_呉市的場3丁目_crosssection2']
129
+ ws = wb['A']
130
- ws.title = 'SP_s1945_呉市的場3丁目_crosssection2'
130
+ ws.title = 'A'
131
131
  #散布図のインスタンスの作成。グラフの大きさ、タイトル、軸ラベル、スタイル指定
132
132
  chart = ScatterChart()
133
- chart.title = "1945 横断図"
133
+ chart.title = "散布図"
134
134
  chart.style = 13
135
- chart.x_axis.title = '下流端からの距離[m]'
135
+ chart.x_axis.title = '[m]'
136
- chart.y_axis.title = '標高[m]'
136
+ chart.y_axis.title = '[m]'
137
137
  chart.height = 15
138
138
  chart.width = 25
139
139
  #列の指定、行の初めの指定、行の終わりの指定(上、左からn番目)
@@ -194,7 +194,7 @@
194
194
  ws.add_chart(chart, "D2")
195
195
 
196
196
  #指定した場所にセーブする
197
- wb.save('1945_matome.xlsx')
197
+ wb.save('matome.xlsx')
198
198
  ```
199
199
 
200
200
  ### 試したこと

1

ループなしでグラフ単体の出力に成功した時のコードを追加しました

2023/03/23 02:26

投稿

squere
squere

スコア2

test CHANGED
File without changes
test CHANGED
@@ -110,6 +110,91 @@
110
110
 
111
111
  #ファイルを保存する
112
112
  wb.save('matome.xlsx')
113
+
114
+ #1つのシートについてグラフ単体の出力に成功したソースコード
115
+ # -*- coding: utf-8 -*-
116
+ #いろいろ必要なモジュールをインポートする
117
+ import openpyxl
118
+ from openpyxl import Workbook, load_workbook
119
+ from openpyxl.chart import ScatterChart, LineChart, Reference, Series
120
+ from openpyxl.chart.shapes import GraphicalProperties
121
+ from openpyxl.chart.layout import Layout, ManualLayout
122
+ from openpyxl.chart.text import RichText
123
+ from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font, RichTextProperties
124
+ from openpyxl.drawing.line import LineProperties
125
+ from copy import deepcopy
126
+
127
+ #使うエクセルファイル、シートを指定する。ワークシートの名前も指定する。
128
+ wb = load_workbook('1945_matome.xlsx')
129
+ ws = wb['SP_s1945_呉市的場3丁目_crosssection2']
130
+ ws.title = 'SP_s1945_呉市的場3丁目_crosssection2'
131
+ #散布図のインスタンスの作成。グラフの大きさ、タイトル、軸ラベル、スタイル指定
132
+ chart = ScatterChart()
133
+ chart.title = "1945 横断図"
134
+ chart.style = 13
135
+ chart.x_axis.title = '下流端からの距離[m]'
136
+ chart.y_axis.title = '標高[m]'
137
+ chart.height = 15
138
+ chart.width = 25
139
+ #列の指定、行の初めの指定、行の終わりの指定(上、左からn番目)
140
+ xvalues = Reference(ws, min_col=2, min_row=2, max_row=28)
141
+ values = Reference(ws, min_col=3, min_row=2, max_row=28)
142
+ #グラフ化
143
+ series = Series(values, xvalues, title_from_data=True)
144
+ #作成したチャートに追加
145
+ chart.series.append(series)
146
+ #マーカーの種類・サイズ・色合いを変える。ラインの色を変える
147
+ chart.series[0].marker.symbol = "triangle"
148
+ chart.series[0].marker.size = 8
149
+ chart.series[0].marker.graphicalProperties.solidFill = "FF0000"
150
+ chart.series[0].marker.graphicalProperties.line.solidFill = "0000FF"
151
+ chart.series[0].graphicalProperties.line.solidFill = "0000FF"
152
+
153
+
154
+
155
+ #XYそれぞれの軸の最大・最少を指定し、グラフの範囲を規定する。また、目盛の間隔を指定する
156
+ chart.x_axis.scaling.min = 0
157
+ chart.y_axis.scaling.min = 0
158
+ chart.x_axis.scaling.max = 250
159
+ chart.y_axis.scaling.max = 250
160
+ chart.x_axis.majorUnit = 25
161
+ chart.y_axis.majorUnit = 50
162
+ #X軸とY軸のフォーマットを変更する それぞれの色やプロットの種類 wは謎
163
+ chart.x_axis.tickLblPos = "low"
164
+ chart.x_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700, prstDash="dot"))
165
+ chart.y_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700))
166
+ #XYのグリッド線をオフにする
167
+ chart.x_axis.majorGridLines = None
168
+ chart.y_axis.majorGridLines = None
169
+ #タイトルとx軸, y軸のラベルの書式を変える
170
+ char_properties = CharacterProperties(latin=Font(typeface='Meiryo UI'), sz=1400, b=True, solidFill="000000")
171
+ paragraph_properties = ParagraphProperties(defRPr=char_properties)
172
+ chart.title.tx.rich.p[0].pPr = paragraph_properties
173
+ chart.x_axis.title.tx.rich.p[0].pPr = paragraph_properties
174
+ chart.y_axis.title.tx.rich.p[0].pPr = paragraph_properties
175
+
176
+ chart.x_axis.title.layout = Layout(ManualLayout(x = 0.45, y = 0.92, xMode="edge", yMode="edge"))
177
+ chart.y_axis.title.layout = Layout(ManualLayout(x = 0.02, y = 0.5, xMode="edge", yMode="edge"))
178
+
179
+ #凡例のフォーマットを変更する リッチテキストとして取り扱う
180
+ char_properties = CharacterProperties(latin=Font(typeface='Meiryo UI'), sz=800, b=False)
181
+ paragraph_properties = ParagraphProperties(defRPr=char_properties)
182
+ rich_text = RichText(p=[Paragraph(pPr=paragraph_properties, endParaRPr=char_properties)])
183
+ chart.legend.txPr = rich_text
184
+
185
+ #凡例の置く位置を変える 最少が0、最大が1 弄ってみるしかない
186
+ chart.legend.layout = Layout(manualLayout = ManualLayout(
187
+ yMode='edge',
188
+ xMode='edge',
189
+ x=0.9, y=0.1,
190
+ h=0.1, w=0.1
191
+ ))
192
+
193
+ #グラフを置く位置をセルで指定する。
194
+ ws.add_chart(chart, "D2")
195
+
196
+ #指定した場所にセーブする
197
+ wb.save('1945_matome.xlsx')
113
198
  ```
114
199
 
115
200
  ### 試したこと