OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I make a 3D bar graph and plot it on excel using data where the negative values on the graph will be colored as well?

  • Thread starter Thread starter Dynoxic
  • Start date Start date
D

Dynoxic

Guest
I am trying to use data from an excel, now hardcoded, and create a 3D graph from it. However, all of my graphs are bringing back colored graphs where the negative values (bars) are black and white. Is there a way I can have the bars colored the same way the positive values are?

This is my code:

Code:
import openpyxl
from openpyxl.chart import (
Reference,
Series,
BarChart3D,
)

wb = Workbook()
ws = wb.active

rows = [
(None, 2013, 2014),
("Apples", -5, -4),
("Oranges", 6, -2),
("Pears", -8, 3)
]

for row in rows:
ws.append(row)

data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)
titles = Reference(ws, min_col=1, min_row=2, max_row=4)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.style = 2
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)
chart.series[0].graphicalProperties.solidFill = 'FFFF66'

ws.add_chart(chart, "E5")
wb.save("bar3d.xlsx")

I have attempted the following:

  • Changing the style of the graph with chart.style
  • Converting the values from negative to positive but I am also appending this data to the excel and doing other calculations so I need the original format
  • Creating a temporary file and deleting it (I don't want another excel saved or hidden), but this would delete the pointer for the graph

Any solutions are helpful.
<p>I am trying to use data from an excel, now hardcoded, and create a 3D graph from it. However, all of my graphs are bringing back colored graphs where the negative values (bars) are black and white. Is there a way I can have the bars colored the same way the positive values are?</p>
<p>This is my code:</p>
<pre><code>import openpyxl
from openpyxl.chart import (
Reference,
Series,
BarChart3D,
)

wb = Workbook()
ws = wb.active

rows = [
(None, 2013, 2014),
("Apples", -5, -4),
("Oranges", 6, -2),
("Pears", -8, 3)
]

for row in rows:
ws.append(row)

data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=4)
titles = Reference(ws, min_col=1, min_row=2, max_row=4)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.style = 2
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)
chart.series[0].graphicalProperties.solidFill = 'FFFF66'

ws.add_chart(chart, "E5")
wb.save("bar3d.xlsx")
</code></pre>
<p>I have attempted the following:</p>
<ul>
<li>Changing the style of the graph with chart.style</li>
<li>Converting the values from negative to positive but I am also appending this data to the excel and doing other calculations so I need the original format</li>
<li>Creating a temporary file and deleting it (I don't want another excel saved or hidden), but this would delete the pointer for the graph</li>
</ul>
<p>Any solutions are helpful.</p>
 
Top