xlsxwriter can not make the chart wider - xlsxwriter

I have create a chart by calling
worksheet.insert_chart(chart_row, chart, {'x_scale': 2, 'y_scale': 1})
it is a stacked type.
I want to make the chart horizontal wider, I tried to change x_scale to a big value, it does not do anything to the chart. when I changed it to 0.5, the chart does shrink. it looks like 1, 2, 3,4 value do not make any impact the chart.
Any ideas?
Thanks

It should work. Here is an example:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
chart1 = workbook.add_chart({'type': 'column'})
chart2 = workbook.add_chart({'type': 'column'})
data = [2, 4, 6, 8, 4]
worksheet.write_column('A1', data)
chart1.add_series({'values': '=Sheet1!$A$1:$A$5'})
chart2.add_series({'values': '=Sheet1!$A$1:$A$5'})
worksheet.insert_chart('C1', chart1)
worksheet.insert_chart('C16', chart2, {'x_scale': 2, 'y_scale': 1})
workbook.close()
And here is the output:

Related

Same Label displayed for Multiple Data Sets? (MatPlotLib, Python 3.9)

Is it possible to assign data sets to existing data labels with MatPlotLib?
Even if the data sets are assigned the same label name & color, in the actual resulting plot, in the legend they are separate, and have different colors. I'm using Python 3.9.
Let's say we have the following code:
import matplotlib.pyplot as plt
dataX = [[0, 1, 2], [0, 1, 2]]
dataY = [[0, 1, 2], [2, 1, 0]]
dataLabel = "Test Data"
for i in range(len(dataX)):
plt.plot(dataX[i], dataY[i], label = dataLabel)
plt.legend(loc = 'best')
plt.show()
plt.close()
This results in the following plot:
And what I want is for both of these data lines to be the same color, and have them both be labeled as the same "Test Data" label instance.
Is what I'm trying to achieve here even possible with MatPlotLib?
Thanks for reading my post, any help is appreciated!
Define a color:
color = 'blue'
Set this color in plot function. Set label to first line only.
line = plt.plot(dataX[0], dataY[0], label=dataLabel, c=color)
for i in range(1, len(dataX)):
plt.plot(dataX[i], dataY[i], c=color)
plt.legend(loc='best', handles=[line[0]])

mouse-over only on actual data points

Here's a really simple line chart.
%matplotlib notebook
import matplotlib.pyplot as plt
lines = plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.setp(lines,marker='D')
plt.ylabel('foo')
plt.xlabel('bar')
plt.show()
If I move my mouse over the chart, I get the x and y values for wherever the pointer is. Is there any way to only get values only when I'm actually over a data point?
I understood you wanted to modify the behavior of the coordinates displayed in the status bar at the bottom right of the plot, is that right?
If so, you can "hijack" the Axes.format_coord() function to make it display whatever you want. You can see an example of this on matplotlib's example gallery.
In your case, something like this seem to do the trick?
my_x = np.array([1, 2, 3, 4])
my_y = np.array([1, 4, 9, 16])
eps = 0.1
def format_coord(x, y):
close_x = np.isclose(my_x, x, atol=eps)
close_y = np.isclose(my_y, y, atol=eps)
if np.any(close_x) and np.any(close_y):
return 'x=%s y=%s' % (ax.format_xdata(my_x[close_x]), ax.format_ydata(my_y[close_y]))
else:
return ''
fig, ax = plt.subplots()
ax.plot(my_x, my_y, 'D-')
ax.set_ylabel('foo')
ax.set_xlabel('bar')
ax.format_coord = format_coord
plt.show()

xlsxwriter: modifying parts of a line in a scatter chart

in the Python package xlsxwriter, is it possible to format a part of a scatter chart series differently than another part? for example, a scatter chart where some sections of the line of a specific series are blue, and other sections of the same line are red. it is certainly possible in Excel itself by modifying specific data points.
I tried using the 'points' option in many combinations without success. I don't know which options are valid for it in a scatter chart.
UPDATE:
here is an example of what I'm trying to achieve. This was created directly in Excel, not through xlsxwriter. Notice how one section of the line is dashed and red, and another is a different thickness. To create it, simply select a data point and use the options in the sidebar to adjust formatting.
I've made an example that I think answers your question.
I'm using Python 3.5 and xlsxwriter 0.9.6.
In chart 1, I changed the color of the markers based on whether they were in a particular group. It's fairly straightforward if chart 1 is what you're looking for.
In chart 2, I show how to hard code a continuous line with different colors (there may be a better way to do this).
import xlsxwriter
import numpy as np
import pandas as pd
dates = pd.DataFrame({'excel_date':pd.date_range('1/1/2016', periods=12, freq='M')})
dates.excel_date = dates.excel_date - pd.datetime(1899, 12, 31)
data = np.array([11,20,25,35,40,48,44,31,25,38,49,60])
selection = np.array([4,5,6,8,11])
#Creating a list - you could hard code these lines if you prefer depending on the size of your series
diff_color_list = list()
for n in range(1, 13):
if n in selection:
diff_color_list.append({'fill':{'color': 'blue', 'width': 3.25}},)
else:
diff_color_list.append({'fill':{'color': 'red', 'width': 3.25}},)
#Workbook Creation
workbook = xlsxwriter.Workbook("test.xlsx")
format = workbook.add_format({'num_format':'mmm-yy'})
worksheet1 = workbook.add_worksheet("testsheet")
worksheet1.write('A1', 'Date')
worksheet1.write('B1', 'Data')
worksheet1.write_column('A2', dates.excel_date, format)
worksheet1.write_column('B2', data)
chart1 = workbook.add_chart({'type': 'scatter'})
# Configure the series.
chart1.add_series({'categories': '=testsheet!$A$2:$A$13',
'values': '=testsheet!$B$2:$B$13',
'points': diff_color_list
})
chart1.set_title ({'name': 'Results'})
chart1.set_x_axis({'name': 'Date'})
chart1.set_y_axis({'name': 'Data'})
chart1.set_legend({'none': True})
# Second chart with alternating line colors
chart2 = workbook.add_chart({'type': 'scatter',
'subtype': 'straight'})
chart2.add_series({'categories': '=testsheet!$A$2:$A$3',
'values': '=testsheet!$B$2:$B$3',
'line':{'color': 'blue'}
})
chart2.add_series({'categories': '=testsheet!$A$3:$A$4',
'values': '=testsheet!$B$3:$B$4',
'line':{'color': 'red'}
})
chart2.add_series({'categories': '=testsheet!$A$4:$A$5',
'values': '=testsheet!$B$4:$B$5',
'line':{'color': 'blue'}
})
chart2.set_title ({'name': 'Results'})
chart2.set_x_axis({'name': 'Date'})
chart2.set_y_axis({'name': 'Data'})
chart2.set_legend({'none': True})
worksheet1.insert_chart('D6', chart1)
worksheet1.insert_chart('L6', chart2)
workbook.close()
The question is a little bit confusing since you talk about changing the color of parts of a line but also about points.
I'm going to assume you are referring to changing the color of points/markers since as far as I know changing the color of line segments in a series isn't possible in Excel.
Anyway, it it possible to change marker colors in a scatter chart using XlsxWriter. For example:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_scatter.xlsx')
worksheet = workbook.add_worksheet()
# Add the worksheet data that the charts will refer to.
worksheet.write_column('A1', [1, 2, 3, 4, 5, 6])
worksheet.write_column('B1', [15, 40, 50, 20, 10, 50])
# Create a new scatter chart.
chart = workbook.add_chart({'type': 'scatter',
'subtype': 'straight_with_markers'})
# Configure the chart series. Increase the default marker size for clarity
# and configure the series points to
chart.add_series({
'categories': '=Sheet1!$A$1:$A$6',
'values': '=Sheet1!$B$1:$B$6',
'marker': {'type': 'square',
'size': 12},
'points': [
None,
None,
{'fill': {'color': 'green'},
'border': {'color': 'black'}},
None,
{'fill': {'color': 'red'},
'border': {'color': 'black'}},
],
})
# Turn off the legend for clarity.
chart.set_legend({'none': True})
# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)
workbook.close()
Output:

Use radar chart in a subplot

I used this code to produce a radar chart: Radar chart with multiple scales on multiple axes; now I want to place this chart in bottom right corner of a 2x1 figures set-up. Using the code below:
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels)
pl.subplot(2, 1, 1)
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
pl.subplot(2, 1, 2)
radar.plot([3, 6, 4, 1, 1, 2], "-", lw=2, color="y", alpha=0.4, label="second")
this yields two blank boxes, while I would like to get two radar charts, one above the other (see link below).
[1]: http://i.stack.imgur.com/oaXzf.png - two blank boxes
If I try to create a single radar chart, the code works correctly (see code and link below):
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels)
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
radar.ax.legend()
[2]: http://i.stack.imgur.com/LnL6e.png - radar chart working correctly
How can I get the two radar charts one above the other? Or how can I insert the radar in a subplot while the other subplots show different kind of chart?
Since you are using the Radar class given in HYRY's answer in Radar chart with multiple scales on multiple axes, here's a solution using that:
fig = pl.figure(figsize=(5, 5))
titles = ['A','B','C','D','E','F']
parameters_list = ['','2','','4','','6','','8','','10']
labels = [parameters_list, parameters_list, parameters_list,parameters_list,parameters_list,parameters_list]
radar = Radar(fig, titles, labels, rect=[0.0, 0.55, 0.5, 0.45])
radar.plot([1, 3, 2, 5, 4, 9], "-", lw=2, color="r", alpha=0.4, label="first")
radar = Radar(fig, titles, labels, rect=[0.0, 0.0, 0.5, 0.45])
radar.plot([3, 6, 4, 1, 1, 2], "-", lw=2, color="y", alpha=0.4, label="second")
Result:
I used the optional rect parameter in that class, which provides sizes for [left, bottom, width, height] relative to the full figure.
Having done so, I don't know why you chose to use a class that is meant for displaying multiple scales, since you only seem to have a single scale (duplicated 6 times in the labels array), so I have to assume you have a good reason.

How can I use XlsxWriter to plot a barchart and fill in different colors for each of the bar in the same series

For example, I have 3 data elements (49, 23, 40 for example) for Jan, Feb and Mar , and they all belong to the same series 1. When I plot them in a bar chart, they can only be filled by the same color in XlsxWriter. What can I do to fill them with different colors? Thanks!
In general in XlsxWriter (and Excel) you can set the colour for a bar chart using the line and/or fill properties.
To set the colour for each bar you need to set the colour for each point in the series. For example:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_column('A1', ['Jan', 'Feb', 'Mar'])
worksheet.write_column('B1', [49, 23, 40])
chart = workbook.add_chart({'type': 'bar'})
chart.add_series({
'categories': '=Sheet1!$A$1:$A$3',
'values': '=Sheet1!$B$1:$B$3',
'points': [
{'fill': {'color': 'red'}},
{'fill': {'color': 'green'}},
{'fill': {'color': 'blue'}},
],
})
worksheet.insert_chart('B5', chart)
workbook.close()
Output:
See the XlsxWriter documentation on chart series point options and Working with Colors.